1 /* handling of writes to regular files and writing back to the server
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
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.
11 #include <linux/backing-dev.h>
12 #include <linux/slab.h>
14 #include <linux/pagemap.h>
15 #include <linux/writeback.h>
16 #include <linux/pagevec.h>
19 static int afs_write_back_from_locked_page(struct afs_writeback *wb,
23 * mark a page as having been made dirty and thus needing writeback
25 int afs_set_page_dirty(struct page *page)
28 return __set_page_dirty_nobuffers(page);
32 * unlink a writeback record because its usage has reached zero
33 * - must be called with the wb->vnode->writeback_lock held
35 static void afs_unlink_writeback(struct afs_writeback *wb)
37 struct afs_writeback *front;
38 struct afs_vnode *vnode = wb->vnode;
40 list_del_init(&wb->link);
41 if (!list_empty(&vnode->writebacks)) {
42 /* if an fsync rises to the front of the queue then wake it
44 front = list_entry(vnode->writebacks.next,
45 struct afs_writeback, link);
46 if (front->state == AFS_WBACK_SYNCING) {
47 _debug("wake up sync");
48 front->state = AFS_WBACK_COMPLETE;
49 wake_up(&front->waitq);
55 * free a writeback record
57 static void afs_free_writeback(struct afs_writeback *wb)
65 * dispose of a reference to a writeback record
67 void afs_put_writeback(struct afs_writeback *wb)
69 struct afs_vnode *vnode = wb->vnode;
71 _enter("{%d}", wb->usage);
73 spin_lock(&vnode->writeback_lock);
75 afs_unlink_writeback(wb);
78 spin_unlock(&vnode->writeback_lock);
80 afs_free_writeback(wb);
84 * partly or wholly fill a page that's under preparation for writing
86 static int afs_fill_page(struct afs_vnode *vnode, struct key *key,
87 loff_t pos, struct page *page)
93 _enter(",,%llu", (unsigned long long)pos);
95 req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *),
100 atomic_set(&req->usage, 1);
103 req->pages[0] = page;
105 i_size = i_size_read(&vnode->vfs_inode);
106 if (pos + PAGE_SIZE > i_size)
107 req->len = i_size - pos;
109 req->len = PAGE_SIZE;
111 ret = afs_vnode_fetch_data(vnode, key, req);
114 if (ret == -ENOENT) {
115 _debug("got NOENT from server"
116 " - marking file deleted and stale");
117 set_bit(AFS_VNODE_DELETED, &vnode->flags);
122 _leave(" = %d", ret);
127 * prepare to perform part of a write to a page
129 int afs_write_begin(struct file *file, struct address_space *mapping,
130 loff_t pos, unsigned len, unsigned flags,
131 struct page **pagep, void **fsdata)
133 struct afs_writeback *candidate, *wb;
134 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
136 struct key *key = file->private_data;
137 unsigned from = pos & (PAGE_SIZE - 1);
138 unsigned to = from + len;
139 pgoff_t index = pos >> PAGE_SHIFT;
142 _enter("{%x:%u},{%lx},%u,%u",
143 vnode->fid.vid, vnode->fid.vnode, index, from, to);
145 candidate = kzalloc(sizeof(*candidate), GFP_KERNEL);
148 candidate->vnode = vnode;
149 candidate->first = candidate->last = index;
150 candidate->offset_first = from;
151 candidate->to_last = to;
152 INIT_LIST_HEAD(&candidate->link);
153 candidate->usage = 1;
154 candidate->state = AFS_WBACK_PENDING;
155 init_waitqueue_head(&candidate->waitq);
157 page = grab_cache_page_write_begin(mapping, index, flags);
163 /* page won't leak in error case: it eventually gets cleaned off LRU */
165 if (!PageUptodate(page) && len != PAGE_SIZE) {
166 ret = afs_fill_page(vnode, key, index << PAGE_SHIFT, page);
169 _leave(" = %d [prep]", ret);
172 SetPageUptodate(page);
176 spin_lock(&vnode->writeback_lock);
178 /* see if this page is already pending a writeback under a suitable key
179 * - if so we can just join onto that one */
180 wb = (struct afs_writeback *) page_private(page);
182 if (wb->key == key && wb->state == AFS_WBACK_PENDING)
183 goto subsume_in_current_wb;
184 goto flush_conflicting_wb;
188 /* see if we can find an already pending writeback that we can
189 * append this page to */
190 list_for_each_entry(wb, &vnode->writebacks, link) {
191 if (wb->last == index - 1 && wb->key == key &&
192 wb->state == AFS_WBACK_PENDING)
193 goto append_to_previous_wb;
197 list_add_tail(&candidate->link, &vnode->writebacks);
198 candidate->key = key_get(key);
199 spin_unlock(&vnode->writeback_lock);
200 SetPagePrivate(page);
201 set_page_private(page, (unsigned long) candidate);
202 _leave(" = 0 [new]");
205 subsume_in_current_wb:
207 ASSERTRANGE(wb->first, <=, index, <=, wb->last);
208 if (index == wb->first && from < wb->offset_first)
209 wb->offset_first = from;
210 if (index == wb->last && to > wb->to_last)
212 spin_unlock(&vnode->writeback_lock);
214 _leave(" = 0 [sub]");
217 append_to_previous_wb:
218 _debug("append into %lx-%lx", wb->first, wb->last);
222 spin_unlock(&vnode->writeback_lock);
223 SetPagePrivate(page);
224 set_page_private(page, (unsigned long) wb);
226 _leave(" = 0 [app]");
229 /* the page is currently bound to another context, so if it's dirty we
230 * need to flush it before we can use the new context */
231 flush_conflicting_wb:
232 _debug("flush conflict");
233 if (wb->state == AFS_WBACK_PENDING)
234 wb->state = AFS_WBACK_CONFLICTING;
235 spin_unlock(&vnode->writeback_lock);
236 if (PageDirty(page)) {
237 ret = afs_write_back_from_locked_page(wb, page);
239 afs_put_writeback(candidate);
240 _leave(" = %d", ret);
245 /* the page holds a ref on the writeback record */
246 afs_put_writeback(wb);
247 set_page_private(page, 0);
248 ClearPagePrivate(page);
253 * finalise part of a write to a page
255 int afs_write_end(struct file *file, struct address_space *mapping,
256 loff_t pos, unsigned len, unsigned copied,
257 struct page *page, void *fsdata)
259 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
260 loff_t i_size, maybe_i_size;
262 _enter("{%x:%u},{%lx}",
263 vnode->fid.vid, vnode->fid.vnode, page->index);
265 maybe_i_size = pos + copied;
267 i_size = i_size_read(&vnode->vfs_inode);
268 if (maybe_i_size > i_size) {
269 spin_lock(&vnode->writeback_lock);
270 i_size = i_size_read(&vnode->vfs_inode);
271 if (maybe_i_size > i_size)
272 i_size_write(&vnode->vfs_inode, maybe_i_size);
273 spin_unlock(&vnode->writeback_lock);
276 set_page_dirty(page);
286 * kill all the pages in the given range
288 static void afs_kill_pages(struct afs_vnode *vnode, bool error,
289 pgoff_t first, pgoff_t last)
292 unsigned count, loop;
294 _enter("{%x:%u},%lx-%lx",
295 vnode->fid.vid, vnode->fid.vnode, first, last);
297 pagevec_init(&pv, 0);
300 _debug("kill %lx-%lx", first, last);
302 count = last - first + 1;
303 if (count > PAGEVEC_SIZE)
304 count = PAGEVEC_SIZE;
305 pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping,
306 first, count, pv.pages);
307 ASSERTCMP(pv.nr, ==, count);
309 for (loop = 0; loop < count; loop++) {
310 ClearPageUptodate(pv.pages[loop]);
312 SetPageError(pv.pages[loop]);
313 end_page_writeback(pv.pages[loop]);
316 __pagevec_release(&pv);
317 } while (first < last);
323 * synchronously write back the locked page and any subsequent non-locked dirty
324 * pages also covered by the same writeback record
326 static int afs_write_back_from_locked_page(struct afs_writeback *wb,
327 struct page *primary_page)
329 struct page *pages[8], *page;
331 unsigned n, offset, to;
332 pgoff_t start, first, last;
335 _enter(",%lx", primary_page->index);
338 if (!clear_page_dirty_for_io(primary_page))
340 if (test_set_page_writeback(primary_page))
343 /* find all consecutive lockable dirty pages, stopping when we find a
344 * page that is not immediately lockable, is not dirty or is missing,
345 * or we reach the end of the range */
346 start = primary_page->index;
347 if (start >= wb->last)
351 _debug("more %lx [%lx]", start, count);
352 n = wb->last - start + 1;
353 if (n > ARRAY_SIZE(pages))
354 n = ARRAY_SIZE(pages);
355 n = find_get_pages_contig(wb->vnode->vfs_inode.i_mapping,
357 _debug("fgpc %u", n);
360 if (pages[0]->index != start) {
362 put_page(pages[--n]);
367 for (loop = 0; loop < n; loop++) {
369 if (page->index > wb->last)
371 if (!trylock_page(page))
373 if (!PageDirty(page) ||
374 page_private(page) != (unsigned long) wb) {
378 if (!clear_page_dirty_for_io(page))
380 if (test_set_page_writeback(page))
387 for (; loop < n; loop++)
388 put_page(pages[loop]);
393 } while (start <= wb->last && count < 65536);
396 /* we now have a contiguous set of dirty pages, each with writeback set
397 * and the dirty mark cleared; the first page is locked and must remain
398 * so, all the rest are unlocked */
399 first = primary_page->index;
400 last = first + count - 1;
402 offset = (first == wb->first) ? wb->offset_first : 0;
403 to = (last == wb->last) ? wb->to_last : PAGE_SIZE;
405 _debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to);
407 ret = afs_vnode_store_data(wb, first, last, offset, to);
412 mapping_set_error(wb->vnode->vfs_inode.i_mapping, -ENOSPC);
421 afs_kill_pages(wb->vnode, true, first, last);
422 mapping_set_error(wb->vnode->vfs_inode.i_mapping, -EIO);
430 afs_kill_pages(wb->vnode, false, first, last);
439 _leave(" = %d", ret);
444 * write a page back to the server
445 * - the caller locked the page for us
447 int afs_writepage(struct page *page, struct writeback_control *wbc)
449 struct afs_writeback *wb;
452 _enter("{%lx},", page->index);
454 wb = (struct afs_writeback *) page_private(page);
457 ret = afs_write_back_from_locked_page(wb, page);
460 _leave(" = %d", ret);
464 wbc->nr_to_write -= ret;
471 * write a region of pages back to the server
473 static int afs_writepages_region(struct address_space *mapping,
474 struct writeback_control *wbc,
475 pgoff_t index, pgoff_t end, pgoff_t *_next)
477 struct afs_writeback *wb;
481 _enter(",,%lx,%lx,", index, end);
484 n = find_get_pages_tag(mapping, &index, PAGECACHE_TAG_DIRTY,
489 _debug("wback %lx", page->index);
491 if (page->index > end) {
494 _leave(" = 0 [%lx]", *_next);
498 /* at this point we hold neither mapping->tree_lock nor lock on
499 * the page itself: the page may be truncated or invalidated
500 * (changing page->mapping to NULL), or even swizzled back from
501 * swapper_space to tmpfs file mapping
505 if (page->mapping != mapping) {
511 if (wbc->sync_mode != WB_SYNC_NONE)
512 wait_on_page_writeback(page);
514 if (PageWriteback(page) || !PageDirty(page)) {
519 wb = (struct afs_writeback *) page_private(page);
522 spin_lock(&wb->vnode->writeback_lock);
523 wb->state = AFS_WBACK_WRITING;
524 spin_unlock(&wb->vnode->writeback_lock);
526 ret = afs_write_back_from_locked_page(wb, page);
530 _leave(" = %d", ret);
534 wbc->nr_to_write -= ret;
537 } while (index < end && wbc->nr_to_write > 0);
540 _leave(" = 0 [%lx]", *_next);
545 * write some of the pending data back to the server
547 int afs_writepages(struct address_space *mapping,
548 struct writeback_control *wbc)
550 pgoff_t start, end, next;
555 if (wbc->range_cyclic) {
556 start = mapping->writeback_index;
558 ret = afs_writepages_region(mapping, wbc, start, end, &next);
559 if (start > 0 && wbc->nr_to_write > 0 && ret == 0)
560 ret = afs_writepages_region(mapping, wbc, 0, start,
562 mapping->writeback_index = next;
563 } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
564 end = (pgoff_t)(LLONG_MAX >> PAGE_SHIFT);
565 ret = afs_writepages_region(mapping, wbc, 0, end, &next);
566 if (wbc->nr_to_write > 0)
567 mapping->writeback_index = next;
569 start = wbc->range_start >> PAGE_SHIFT;
570 end = wbc->range_end >> PAGE_SHIFT;
571 ret = afs_writepages_region(mapping, wbc, start, end, &next);
574 _leave(" = %d", ret);
579 * completion of write to server
581 void afs_pages_written_back(struct afs_vnode *vnode, struct afs_call *call)
583 struct afs_writeback *wb = call->wb;
585 unsigned count, loop;
586 pgoff_t first = call->first, last = call->last;
589 _enter("{%x:%u},{%lx-%lx}",
590 vnode->fid.vid, vnode->fid.vnode, first, last);
594 pagevec_init(&pv, 0);
597 _debug("done %lx-%lx", first, last);
599 count = last - first + 1;
600 if (count > PAGEVEC_SIZE)
601 count = PAGEVEC_SIZE;
602 pv.nr = find_get_pages_contig(call->mapping, first, count,
604 ASSERTCMP(pv.nr, ==, count);
606 spin_lock(&vnode->writeback_lock);
607 for (loop = 0; loop < count; loop++) {
608 struct page *page = pv.pages[loop];
609 end_page_writeback(page);
610 if (page_private(page) == (unsigned long) wb) {
611 set_page_private(page, 0);
612 ClearPagePrivate(page);
617 if (wb->usage == 0) {
618 afs_unlink_writeback(wb);
621 spin_unlock(&vnode->writeback_lock);
624 afs_free_writeback(wb);
628 __pagevec_release(&pv);
629 } while (first <= last);
635 * write to an AFS file
637 ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from)
639 struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
641 size_t count = iov_iter_count(from);
643 _enter("{%x.%u},{%zu},",
644 vnode->fid.vid, vnode->fid.vnode, count);
646 if (IS_SWAPFILE(&vnode->vfs_inode)) {
648 "AFS: Attempt to write to active swap file!\n");
655 result = generic_file_write_iter(iocb, from);
657 _leave(" = %zd", result);
662 * flush the vnode to the fileserver
664 int afs_writeback_all(struct afs_vnode *vnode)
666 struct address_space *mapping = vnode->vfs_inode.i_mapping;
667 struct writeback_control wbc = {
668 .sync_mode = WB_SYNC_ALL,
669 .nr_to_write = LONG_MAX,
676 ret = mapping->a_ops->writepages(mapping, &wbc);
677 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
679 _leave(" = %d", ret);
684 * flush any dirty pages for this process, and check for write errors.
685 * - the return status from this call provides a reliable indication of
686 * whether any write errors occurred for this process.
688 int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
690 struct inode *inode = file_inode(file);
691 struct afs_writeback *wb, *xwb;
692 struct afs_vnode *vnode = AFS_FS_I(inode);
695 _enter("{%x:%u},{n=%pD},%d",
696 vnode->fid.vid, vnode->fid.vnode, file,
699 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
704 /* use a writeback record as a marker in the queue - when this reaches
705 * the front of the queue, all the outstanding writes are either
706 * completed or rejected */
707 wb = kzalloc(sizeof(*wb), GFP_KERNEL);
715 wb->offset_first = 0;
716 wb->to_last = PAGE_SIZE;
718 wb->state = AFS_WBACK_SYNCING;
719 init_waitqueue_head(&wb->waitq);
721 spin_lock(&vnode->writeback_lock);
722 list_for_each_entry(xwb, &vnode->writebacks, link) {
723 if (xwb->state == AFS_WBACK_PENDING)
724 xwb->state = AFS_WBACK_CONFLICTING;
726 list_add_tail(&wb->link, &vnode->writebacks);
727 spin_unlock(&vnode->writeback_lock);
729 /* push all the outstanding writebacks to the server */
730 ret = afs_writeback_all(vnode);
732 afs_put_writeback(wb);
733 _leave(" = %d [wb]", ret);
737 /* wait for the preceding writes to actually complete */
738 ret = wait_event_interruptible(wb->waitq,
739 wb->state == AFS_WBACK_COMPLETE ||
740 vnode->writebacks.next == &wb->link);
741 afs_put_writeback(wb);
742 _leave(" = %d", ret);
749 * notification that a previously read-only page is about to become writable
750 * - if it returns an error, the caller will deliver a bus error signal
752 int afs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
754 struct afs_vnode *vnode = AFS_FS_I(vma->vm_file->f_mapping->host);
756 _enter("{{%x:%u}},{%lx}",
757 vnode->fid.vid, vnode->fid.vnode, page->index);
759 /* wait for the page to be written to the cache before we allow it to
761 #ifdef CONFIG_AFS_FSCACHE
762 fscache_wait_on_page_write(vnode->cache, page);