Merge tag 'afs-next-20171113' of git://git.kernel.org/pub/scm/linux/kernel/git/dhowel...
[sfrench/cifs-2.6.git] / fs / afs / write.c
1 /* handling of writes to regular files and writing back to the server
2  *
3  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11
12 #include <linux/backing-dev.h>
13 #include <linux/slab.h>
14 #include <linux/fs.h>
15 #include <linux/pagemap.h>
16 #include <linux/writeback.h>
17 #include <linux/pagevec.h>
18 #include "internal.h"
19
20 /*
21  * mark a page as having been made dirty and thus needing writeback
22  */
23 int afs_set_page_dirty(struct page *page)
24 {
25         _enter("");
26         return __set_page_dirty_nobuffers(page);
27 }
28
29 /*
30  * partly or wholly fill a page that's under preparation for writing
31  */
32 static int afs_fill_page(struct afs_vnode *vnode, struct key *key,
33                          loff_t pos, unsigned int len, struct page *page)
34 {
35         struct afs_read *req;
36         int ret;
37
38         _enter(",,%llu", (unsigned long long)pos);
39
40         req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *),
41                       GFP_KERNEL);
42         if (!req)
43                 return -ENOMEM;
44
45         atomic_set(&req->usage, 1);
46         req->pos = pos;
47         req->len = len;
48         req->nr_pages = 1;
49         req->pages[0] = page;
50         get_page(page);
51
52         ret = afs_fetch_data(vnode, key, req);
53         afs_put_read(req);
54         if (ret < 0) {
55                 if (ret == -ENOENT) {
56                         _debug("got NOENT from server"
57                                " - marking file deleted and stale");
58                         set_bit(AFS_VNODE_DELETED, &vnode->flags);
59                         ret = -ESTALE;
60                 }
61         }
62
63         _leave(" = %d", ret);
64         return ret;
65 }
66
67 /*
68  * prepare to perform part of a write to a page
69  */
70 int afs_write_begin(struct file *file, struct address_space *mapping,
71                     loff_t pos, unsigned len, unsigned flags,
72                     struct page **pagep, void **fsdata)
73 {
74         struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
75         struct page *page;
76         struct key *key = afs_file_key(file);
77         unsigned long priv;
78         unsigned f, from = pos & (PAGE_SIZE - 1);
79         unsigned t, to = from + len;
80         pgoff_t index = pos >> PAGE_SHIFT;
81         int ret;
82
83         _enter("{%x:%u},{%lx},%u,%u",
84                vnode->fid.vid, vnode->fid.vnode, index, from, to);
85
86         /* We want to store information about how much of a page is altered in
87          * page->private.
88          */
89         BUILD_BUG_ON(PAGE_SIZE > 32768 && sizeof(page->private) < 8);
90
91         page = grab_cache_page_write_begin(mapping, index, flags);
92         if (!page)
93                 return -ENOMEM;
94
95         if (!PageUptodate(page) && len != PAGE_SIZE) {
96                 ret = afs_fill_page(vnode, key, pos & PAGE_MASK, PAGE_SIZE, page);
97                 if (ret < 0) {
98                         unlock_page(page);
99                         put_page(page);
100                         _leave(" = %d [prep]", ret);
101                         return ret;
102                 }
103                 SetPageUptodate(page);
104         }
105
106         /* page won't leak in error case: it eventually gets cleaned off LRU */
107         *pagep = page;
108
109 try_again:
110         /* See if this page is already partially written in a way that we can
111          * merge the new write with.
112          */
113         t = f = 0;
114         if (PagePrivate(page)) {
115                 priv = page_private(page);
116                 f = priv & AFS_PRIV_MAX;
117                 t = priv >> AFS_PRIV_SHIFT;
118                 ASSERTCMP(f, <=, t);
119         }
120
121         if (f != t) {
122                 if (to < f || from > t)
123                         goto flush_conflicting_write;
124                 if (from < f)
125                         f = from;
126                 if (to > t)
127                         t = to;
128         } else {
129                 f = from;
130                 t = to;
131         }
132
133         priv = (unsigned long)t << AFS_PRIV_SHIFT;
134         priv |= f;
135         trace_afs_page_dirty(vnode, tracepoint_string("begin"),
136                              page->index, priv);
137         SetPagePrivate(page);
138         set_page_private(page, priv);
139         _leave(" = 0");
140         return 0;
141
142         /* The previous write and this write aren't adjacent or overlapping, so
143          * flush the page out.
144          */
145 flush_conflicting_write:
146         _debug("flush conflict");
147         ret = write_one_page(page);
148         if (ret < 0) {
149                 _leave(" = %d", ret);
150                 return ret;
151         }
152
153         ret = lock_page_killable(page);
154         if (ret < 0) {
155                 _leave(" = %d", ret);
156                 return ret;
157         }
158         goto try_again;
159 }
160
161 /*
162  * finalise part of a write to a page
163  */
164 int afs_write_end(struct file *file, struct address_space *mapping,
165                   loff_t pos, unsigned len, unsigned copied,
166                   struct page *page, void *fsdata)
167 {
168         struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
169         struct key *key = afs_file_key(file);
170         loff_t i_size, maybe_i_size;
171         int ret;
172
173         _enter("{%x:%u},{%lx}",
174                vnode->fid.vid, vnode->fid.vnode, page->index);
175
176         maybe_i_size = pos + copied;
177
178         i_size = i_size_read(&vnode->vfs_inode);
179         if (maybe_i_size > i_size) {
180                 spin_lock(&vnode->wb_lock);
181                 i_size = i_size_read(&vnode->vfs_inode);
182                 if (maybe_i_size > i_size)
183                         i_size_write(&vnode->vfs_inode, maybe_i_size);
184                 spin_unlock(&vnode->wb_lock);
185         }
186
187         if (!PageUptodate(page)) {
188                 if (copied < len) {
189                         /* Try and load any missing data from the server.  The
190                          * unmarshalling routine will take care of clearing any
191                          * bits that are beyond the EOF.
192                          */
193                         ret = afs_fill_page(vnode, key, pos + copied,
194                                             len - copied, page);
195                         if (ret < 0)
196                                 return ret;
197                 }
198                 SetPageUptodate(page);
199         }
200
201         set_page_dirty(page);
202         if (PageDirty(page))
203                 _debug("dirtied");
204         unlock_page(page);
205         put_page(page);
206
207         return copied;
208 }
209
210 /*
211  * kill all the pages in the given range
212  */
213 static void afs_kill_pages(struct address_space *mapping,
214                            pgoff_t first, pgoff_t last)
215 {
216         struct afs_vnode *vnode = AFS_FS_I(mapping->host);
217         struct pagevec pv;
218         unsigned count, loop;
219
220         _enter("{%x:%u},%lx-%lx",
221                vnode->fid.vid, vnode->fid.vnode, first, last);
222
223         pagevec_init(&pv);
224
225         do {
226                 _debug("kill %lx-%lx", first, last);
227
228                 count = last - first + 1;
229                 if (count > PAGEVEC_SIZE)
230                         count = PAGEVEC_SIZE;
231                 pv.nr = find_get_pages_contig(mapping, first, count, pv.pages);
232                 ASSERTCMP(pv.nr, ==, count);
233
234                 for (loop = 0; loop < count; loop++) {
235                         struct page *page = pv.pages[loop];
236                         ClearPageUptodate(page);
237                         SetPageError(page);
238                         end_page_writeback(page);
239                         if (page->index >= first)
240                                 first = page->index + 1;
241                         lock_page(page);
242                         generic_error_remove_page(mapping, page);
243                 }
244
245                 __pagevec_release(&pv);
246         } while (first <= last);
247
248         _leave("");
249 }
250
251 /*
252  * Redirty all the pages in a given range.
253  */
254 static void afs_redirty_pages(struct writeback_control *wbc,
255                               struct address_space *mapping,
256                               pgoff_t first, pgoff_t last)
257 {
258         struct afs_vnode *vnode = AFS_FS_I(mapping->host);
259         struct pagevec pv;
260         unsigned count, loop;
261
262         _enter("{%x:%u},%lx-%lx",
263                vnode->fid.vid, vnode->fid.vnode, first, last);
264
265         pagevec_init(&pv);
266
267         do {
268                 _debug("redirty %lx-%lx", first, last);
269
270                 count = last - first + 1;
271                 if (count > PAGEVEC_SIZE)
272                         count = PAGEVEC_SIZE;
273                 pv.nr = find_get_pages_contig(mapping, first, count, pv.pages);
274                 ASSERTCMP(pv.nr, ==, count);
275
276                 for (loop = 0; loop < count; loop++) {
277                         struct page *page = pv.pages[loop];
278
279                         redirty_page_for_writepage(wbc, page);
280                         end_page_writeback(page);
281                         if (page->index >= first)
282                                 first = page->index + 1;
283                 }
284
285                 __pagevec_release(&pv);
286         } while (first <= last);
287
288         _leave("");
289 }
290
291 /*
292  * write to a file
293  */
294 static int afs_store_data(struct address_space *mapping,
295                           pgoff_t first, pgoff_t last,
296                           unsigned offset, unsigned to)
297 {
298         struct afs_vnode *vnode = AFS_FS_I(mapping->host);
299         struct afs_fs_cursor fc;
300         struct afs_wb_key *wbk = NULL;
301         struct list_head *p;
302         int ret = -ENOKEY, ret2;
303
304         _enter("%s{%x:%u.%u},%lx,%lx,%x,%x",
305                vnode->volume->name,
306                vnode->fid.vid,
307                vnode->fid.vnode,
308                vnode->fid.unique,
309                first, last, offset, to);
310
311         spin_lock(&vnode->wb_lock);
312         p = vnode->wb_keys.next;
313
314         /* Iterate through the list looking for a valid key to use. */
315 try_next_key:
316         while (p != &vnode->wb_keys) {
317                 wbk = list_entry(p, struct afs_wb_key, vnode_link);
318                 _debug("wbk %u", key_serial(wbk->key));
319                 ret2 = key_validate(wbk->key);
320                 if (ret2 == 0)
321                         goto found_key;
322                 if (ret == -ENOKEY)
323                         ret = ret2;
324                 p = p->next;
325         }
326
327         spin_unlock(&vnode->wb_lock);
328         afs_put_wb_key(wbk);
329         _leave(" = %d [no keys]", ret);
330         return ret;
331
332 found_key:
333         refcount_inc(&wbk->usage);
334         spin_unlock(&vnode->wb_lock);
335
336         _debug("USE WB KEY %u", key_serial(wbk->key));
337
338         ret = -ERESTARTSYS;
339         if (afs_begin_vnode_operation(&fc, vnode, wbk->key)) {
340                 while (afs_select_fileserver(&fc)) {
341                         fc.cb_break = vnode->cb_break + vnode->cb_s_break;
342                         afs_fs_store_data(&fc, mapping, first, last, offset, to);
343                 }
344
345                 afs_check_for_remote_deletion(&fc, fc.vnode);
346                 afs_vnode_commit_status(&fc, vnode, fc.cb_break);
347                 ret = afs_end_vnode_operation(&fc);
348         }
349
350         switch (ret) {
351         case -EACCES:
352         case -EPERM:
353         case -ENOKEY:
354         case -EKEYEXPIRED:
355         case -EKEYREJECTED:
356         case -EKEYREVOKED:
357                 _debug("next");
358                 spin_lock(&vnode->wb_lock);
359                 p = wbk->vnode_link.next;
360                 afs_put_wb_key(wbk);
361                 goto try_next_key;
362         }
363
364         afs_put_wb_key(wbk);
365         _leave(" = %d", ret);
366         return ret;
367 }
368
369 /*
370  * Synchronously write back the locked page and any subsequent non-locked dirty
371  * pages.
372  */
373 static int afs_write_back_from_locked_page(struct address_space *mapping,
374                                            struct writeback_control *wbc,
375                                            struct page *primary_page,
376                                            pgoff_t final_page)
377 {
378         struct afs_vnode *vnode = AFS_FS_I(mapping->host);
379         struct page *pages[8], *page;
380         unsigned long count, priv;
381         unsigned n, offset, to, f, t;
382         pgoff_t start, first, last;
383         int loop, ret;
384
385         _enter(",%lx", primary_page->index);
386
387         count = 1;
388         if (test_set_page_writeback(primary_page))
389                 BUG();
390
391         /* Find all consecutive lockable dirty pages that have contiguous
392          * written regions, stopping when we find a page that is not
393          * immediately lockable, is not dirty or is missing, or we reach the
394          * end of the range.
395          */
396         start = primary_page->index;
397         priv = page_private(primary_page);
398         offset = priv & AFS_PRIV_MAX;
399         to = priv >> AFS_PRIV_SHIFT;
400         trace_afs_page_dirty(vnode, tracepoint_string("store"),
401                              primary_page->index, priv);
402
403         WARN_ON(offset == to);
404         if (offset == to)
405                 trace_afs_page_dirty(vnode, tracepoint_string("WARN"),
406                                      primary_page->index, priv);
407
408         if (start >= final_page || to < PAGE_SIZE)
409                 goto no_more;
410
411         start++;
412         do {
413                 _debug("more %lx [%lx]", start, count);
414                 n = final_page - start + 1;
415                 if (n > ARRAY_SIZE(pages))
416                         n = ARRAY_SIZE(pages);
417                 n = find_get_pages_contig(mapping, start, ARRAY_SIZE(pages), pages);
418                 _debug("fgpc %u", n);
419                 if (n == 0)
420                         goto no_more;
421                 if (pages[0]->index != start) {
422                         do {
423                                 put_page(pages[--n]);
424                         } while (n > 0);
425                         goto no_more;
426                 }
427
428                 for (loop = 0; loop < n; loop++) {
429                         if (to != PAGE_SIZE)
430                                 break;
431                         page = pages[loop];
432                         if (page->index > final_page)
433                                 break;
434                         if (!trylock_page(page))
435                                 break;
436                         if (!PageDirty(page) || PageWriteback(page)) {
437                                 unlock_page(page);
438                                 break;
439                         }
440
441                         priv = page_private(page);
442                         f = priv & AFS_PRIV_MAX;
443                         t = priv >> AFS_PRIV_SHIFT;
444                         if (f != 0) {
445                                 unlock_page(page);
446                                 break;
447                         }
448                         to = t;
449
450                         trace_afs_page_dirty(vnode, tracepoint_string("store+"),
451                                              page->index, priv);
452
453                         if (!clear_page_dirty_for_io(page))
454                                 BUG();
455                         if (test_set_page_writeback(page))
456                                 BUG();
457                         unlock_page(page);
458                         put_page(page);
459                 }
460                 count += loop;
461                 if (loop < n) {
462                         for (; loop < n; loop++)
463                                 put_page(pages[loop]);
464                         goto no_more;
465                 }
466
467                 start += loop;
468         } while (start <= final_page && count < 65536);
469
470 no_more:
471         /* We now have a contiguous set of dirty pages, each with writeback
472          * set; the first page is still locked at this point, but all the rest
473          * have been unlocked.
474          */
475         unlock_page(primary_page);
476
477         first = primary_page->index;
478         last = first + count - 1;
479
480         _debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to);
481
482         ret = afs_store_data(mapping, first, last, offset, to);
483         switch (ret) {
484         case 0:
485                 ret = count;
486                 break;
487
488         default:
489                 pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret);
490                 /* Fall through */
491         case -EACCES:
492         case -EPERM:
493         case -ENOKEY:
494         case -EKEYEXPIRED:
495         case -EKEYREJECTED:
496         case -EKEYREVOKED:
497                 afs_redirty_pages(wbc, mapping, first, last);
498                 mapping_set_error(mapping, ret);
499                 break;
500
501         case -EDQUOT:
502         case -ENOSPC:
503                 afs_redirty_pages(wbc, mapping, first, last);
504                 mapping_set_error(mapping, -ENOSPC);
505                 break;
506
507         case -EROFS:
508         case -EIO:
509         case -EREMOTEIO:
510         case -EFBIG:
511         case -ENOENT:
512         case -ENOMEDIUM:
513         case -ENXIO:
514                 afs_kill_pages(mapping, first, last);
515                 mapping_set_error(mapping, ret);
516                 break;
517         }
518
519         _leave(" = %d", ret);
520         return ret;
521 }
522
523 /*
524  * write a page back to the server
525  * - the caller locked the page for us
526  */
527 int afs_writepage(struct page *page, struct writeback_control *wbc)
528 {
529         int ret;
530
531         _enter("{%lx},", page->index);
532
533         ret = afs_write_back_from_locked_page(page->mapping, wbc, page,
534                                               wbc->range_end >> PAGE_SHIFT);
535         if (ret < 0) {
536                 _leave(" = %d", ret);
537                 return 0;
538         }
539
540         wbc->nr_to_write -= ret;
541
542         _leave(" = 0");
543         return 0;
544 }
545
546 /*
547  * write a region of pages back to the server
548  */
549 static int afs_writepages_region(struct address_space *mapping,
550                                  struct writeback_control *wbc,
551                                  pgoff_t index, pgoff_t end, pgoff_t *_next)
552 {
553         struct page *page;
554         int ret, n;
555
556         _enter(",,%lx,%lx,", index, end);
557
558         do {
559                 n = find_get_pages_range_tag(mapping, &index, end,
560                                         PAGECACHE_TAG_DIRTY, 1, &page);
561                 if (!n)
562                         break;
563
564                 _debug("wback %lx", page->index);
565
566                 /* at this point we hold neither mapping->tree_lock nor lock on
567                  * the page itself: the page may be truncated or invalidated
568                  * (changing page->mapping to NULL), or even swizzled back from
569                  * swapper_space to tmpfs file mapping
570                  */
571                 ret = lock_page_killable(page);
572                 if (ret < 0) {
573                         put_page(page);
574                         _leave(" = %d", ret);
575                         return ret;
576                 }
577
578                 if (page->mapping != mapping || !PageDirty(page)) {
579                         unlock_page(page);
580                         put_page(page);
581                         continue;
582                 }
583
584                 if (PageWriteback(page)) {
585                         unlock_page(page);
586                         if (wbc->sync_mode != WB_SYNC_NONE)
587                                 wait_on_page_writeback(page);
588                         put_page(page);
589                         continue;
590                 }
591
592                 if (!clear_page_dirty_for_io(page))
593                         BUG();
594                 ret = afs_write_back_from_locked_page(mapping, wbc, page, end);
595                 put_page(page);
596                 if (ret < 0) {
597                         _leave(" = %d", ret);
598                         return ret;
599                 }
600
601                 wbc->nr_to_write -= ret;
602
603                 cond_resched();
604         } while (index < end && wbc->nr_to_write > 0);
605
606         *_next = index;
607         _leave(" = 0 [%lx]", *_next);
608         return 0;
609 }
610
611 /*
612  * write some of the pending data back to the server
613  */
614 int afs_writepages(struct address_space *mapping,
615                    struct writeback_control *wbc)
616 {
617         pgoff_t start, end, next;
618         int ret;
619
620         _enter("");
621
622         if (wbc->range_cyclic) {
623                 start = mapping->writeback_index;
624                 end = -1;
625                 ret = afs_writepages_region(mapping, wbc, start, end, &next);
626                 if (start > 0 && wbc->nr_to_write > 0 && ret == 0)
627                         ret = afs_writepages_region(mapping, wbc, 0, start,
628                                                     &next);
629                 mapping->writeback_index = next;
630         } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
631                 end = (pgoff_t)(LLONG_MAX >> PAGE_SHIFT);
632                 ret = afs_writepages_region(mapping, wbc, 0, end, &next);
633                 if (wbc->nr_to_write > 0)
634                         mapping->writeback_index = next;
635         } else {
636                 start = wbc->range_start >> PAGE_SHIFT;
637                 end = wbc->range_end >> PAGE_SHIFT;
638                 ret = afs_writepages_region(mapping, wbc, start, end, &next);
639         }
640
641         _leave(" = %d", ret);
642         return ret;
643 }
644
645 /*
646  * completion of write to server
647  */
648 void afs_pages_written_back(struct afs_vnode *vnode, struct afs_call *call)
649 {
650         struct pagevec pv;
651         unsigned long priv;
652         unsigned count, loop;
653         pgoff_t first = call->first, last = call->last;
654
655         _enter("{%x:%u},{%lx-%lx}",
656                vnode->fid.vid, vnode->fid.vnode, first, last);
657
658         pagevec_init(&pv);
659
660         do {
661                 _debug("done %lx-%lx", first, last);
662
663                 count = last - first + 1;
664                 if (count > PAGEVEC_SIZE)
665                         count = PAGEVEC_SIZE;
666                 pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping,
667                                               first, count, pv.pages);
668                 ASSERTCMP(pv.nr, ==, count);
669
670                 for (loop = 0; loop < count; loop++) {
671                         priv = page_private(pv.pages[loop]);
672                         trace_afs_page_dirty(vnode, tracepoint_string("clear"),
673                                              pv.pages[loop]->index, priv);
674                         set_page_private(pv.pages[loop], 0);
675                         end_page_writeback(pv.pages[loop]);
676                 }
677                 first += count;
678                 __pagevec_release(&pv);
679         } while (first <= last);
680
681         afs_prune_wb_keys(vnode);
682         _leave("");
683 }
684
685 /*
686  * write to an AFS file
687  */
688 ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from)
689 {
690         struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
691         ssize_t result;
692         size_t count = iov_iter_count(from);
693
694         _enter("{%x.%u},{%zu},",
695                vnode->fid.vid, vnode->fid.vnode, count);
696
697         if (IS_SWAPFILE(&vnode->vfs_inode)) {
698                 printk(KERN_INFO
699                        "AFS: Attempt to write to active swap file!\n");
700                 return -EBUSY;
701         }
702
703         if (!count)
704                 return 0;
705
706         result = generic_file_write_iter(iocb, from);
707
708         _leave(" = %zd", result);
709         return result;
710 }
711
712 /*
713  * flush any dirty pages for this process, and check for write errors.
714  * - the return status from this call provides a reliable indication of
715  *   whether any write errors occurred for this process.
716  */
717 int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
718 {
719         struct inode *inode = file_inode(file);
720         struct afs_vnode *vnode = AFS_FS_I(inode);
721
722         _enter("{%x:%u},{n=%pD},%d",
723                vnode->fid.vid, vnode->fid.vnode, file,
724                datasync);
725
726         return file_write_and_wait_range(file, start, end);
727 }
728
729 /*
730  * Flush out all outstanding writes on a file opened for writing when it is
731  * closed.
732  */
733 int afs_flush(struct file *file, fl_owner_t id)
734 {
735         _enter("");
736
737         if ((file->f_mode & FMODE_WRITE) == 0)
738                 return 0;
739
740         return vfs_fsync(file, 0);
741 }
742
743 /*
744  * notification that a previously read-only page is about to become writable
745  * - if it returns an error, the caller will deliver a bus error signal
746  */
747 int afs_page_mkwrite(struct vm_fault *vmf)
748 {
749         struct file *file = vmf->vma->vm_file;
750         struct inode *inode = file_inode(file);
751         struct afs_vnode *vnode = AFS_FS_I(inode);
752         unsigned long priv;
753
754         _enter("{{%x:%u}},{%lx}",
755                vnode->fid.vid, vnode->fid.vnode, vmf->page->index);
756
757         sb_start_pagefault(inode->i_sb);
758
759         /* Wait for the page to be written to the cache before we allow it to
760          * be modified.  We then assume the entire page will need writing back.
761          */
762 #ifdef CONFIG_AFS_FSCACHE
763         fscache_wait_on_page_write(vnode->cache, vmf->page);
764 #endif
765
766         if (PageWriteback(vmf->page) &&
767             wait_on_page_bit_killable(vmf->page, PG_writeback) < 0)
768                 return VM_FAULT_RETRY;
769
770         if (lock_page_killable(vmf->page) < 0)
771                 return VM_FAULT_RETRY;
772
773         /* We mustn't change page->private until writeback is complete as that
774          * details the portion of the page we need to write back and we might
775          * need to redirty the page if there's a problem.
776          */
777         wait_on_page_writeback(vmf->page);
778
779         priv = (unsigned long)PAGE_SIZE << AFS_PRIV_SHIFT; /* To */
780         priv |= 0; /* From */
781         trace_afs_page_dirty(vnode, tracepoint_string("mkwrite"),
782                              vmf->page->index, priv);
783         SetPagePrivate(vmf->page);
784         set_page_private(vmf->page, priv);
785
786         sb_end_pagefault(inode->i_sb);
787         return VM_FAULT_LOCKED;
788 }
789
790 /*
791  * Prune the keys cached for writeback.  The caller must hold vnode->wb_lock.
792  */
793 void afs_prune_wb_keys(struct afs_vnode *vnode)
794 {
795         LIST_HEAD(graveyard);
796         struct afs_wb_key *wbk, *tmp;
797
798         /* Discard unused keys */
799         spin_lock(&vnode->wb_lock);
800
801         if (!mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_WRITEBACK) &&
802             !mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_DIRTY)) {
803                 list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) {
804                         if (refcount_read(&wbk->usage) == 1)
805                                 list_move(&wbk->vnode_link, &graveyard);
806                 }
807         }
808
809         spin_unlock(&vnode->wb_lock);
810
811         while (!list_empty(&graveyard)) {
812                 wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link);
813                 list_del(&wbk->vnode_link);
814                 afs_put_wb_key(wbk);
815         }
816 }
817
818 /*
819  * Clean up a page during invalidation.
820  */
821 int afs_launder_page(struct page *page)
822 {
823         struct address_space *mapping = page->mapping;
824         struct afs_vnode *vnode = AFS_FS_I(mapping->host);
825         unsigned long priv;
826         unsigned int f, t;
827         int ret = 0;
828
829         _enter("{%lx}", page->index);
830
831         priv = page_private(page);
832         if (clear_page_dirty_for_io(page)) {
833                 f = 0;
834                 t = PAGE_SIZE;
835                 if (PagePrivate(page)) {
836                         f = priv & AFS_PRIV_MAX;
837                         t = priv >> AFS_PRIV_SHIFT;
838                 }
839
840                 trace_afs_page_dirty(vnode, tracepoint_string("launder"),
841                                      page->index, priv);
842                 ret = afs_store_data(mapping, page->index, page->index, t, f);
843         }
844
845         trace_afs_page_dirty(vnode, tracepoint_string("laundered"),
846                              page->index, priv);
847         set_page_private(page, 0);
848         ClearPagePrivate(page);
849
850 #ifdef CONFIG_AFS_FSCACHE
851         if (PageFsCache(page)) {
852                 fscache_wait_on_page_write(vnode->cache, page);
853                 fscache_uncache_page(vnode->cache, page);
854         }
855 #endif
856         return ret;
857 }