ocfs2: fix locking for res->tracking and dlm->tracking_list
[linux/fpc-iii.git] / fs / afs / write.c
blob5cfc05ca184cb845a1c2df464dce05d4a91f56b3
1 /* handling of writes to regular files and writing back to the server
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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>
13 #include <linux/fs.h>
14 #include <linux/pagemap.h>
15 #include <linux/writeback.h>
16 #include <linux/pagevec.h>
17 #include "internal.h"
19 static int afs_write_back_from_locked_page(struct afs_writeback *wb,
20 struct page *page);
23 * mark a page as having been made dirty and thus needing writeback
25 int afs_set_page_dirty(struct page *page)
27 _enter("");
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
43 * up */
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)
59 _enter("");
60 key_put(wb->key);
61 kfree(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);
74 if (--wb->usage == 0)
75 afs_unlink_writeback(wb);
76 else
77 wb = NULL;
78 spin_unlock(&vnode->writeback_lock);
79 if (wb)
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)
89 loff_t i_size;
90 int ret;
91 int len;
93 _enter(",,%llu", (unsigned long long)pos);
95 i_size = i_size_read(&vnode->vfs_inode);
96 if (pos + PAGE_CACHE_SIZE > i_size)
97 len = i_size - pos;
98 else
99 len = PAGE_CACHE_SIZE;
101 ret = afs_vnode_fetch_data(vnode, key, pos, len, page);
102 if (ret < 0) {
103 if (ret == -ENOENT) {
104 _debug("got NOENT from server"
105 " - marking file deleted and stale");
106 set_bit(AFS_VNODE_DELETED, &vnode->flags);
107 ret = -ESTALE;
111 _leave(" = %d", ret);
112 return ret;
116 * prepare to perform part of a write to a page
118 int afs_write_begin(struct file *file, struct address_space *mapping,
119 loff_t pos, unsigned len, unsigned flags,
120 struct page **pagep, void **fsdata)
122 struct afs_writeback *candidate, *wb;
123 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
124 struct page *page;
125 struct key *key = file->private_data;
126 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
127 unsigned to = from + len;
128 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
129 int ret;
131 _enter("{%x:%u},{%lx},%u,%u",
132 vnode->fid.vid, vnode->fid.vnode, index, from, to);
134 candidate = kzalloc(sizeof(*candidate), GFP_KERNEL);
135 if (!candidate)
136 return -ENOMEM;
137 candidate->vnode = vnode;
138 candidate->first = candidate->last = index;
139 candidate->offset_first = from;
140 candidate->to_last = to;
141 INIT_LIST_HEAD(&candidate->link);
142 candidate->usage = 1;
143 candidate->state = AFS_WBACK_PENDING;
144 init_waitqueue_head(&candidate->waitq);
146 page = grab_cache_page_write_begin(mapping, index, flags);
147 if (!page) {
148 kfree(candidate);
149 return -ENOMEM;
152 if (!PageUptodate(page) && len != PAGE_CACHE_SIZE) {
153 ret = afs_fill_page(vnode, key, index << PAGE_CACHE_SHIFT, page);
154 if (ret < 0) {
155 unlock_page(page);
156 put_page(page);
157 kfree(candidate);
158 _leave(" = %d [prep]", ret);
159 return ret;
161 SetPageUptodate(page);
164 /* page won't leak in error case: it eventually gets cleaned off LRU */
165 *pagep = page;
167 try_again:
168 spin_lock(&vnode->writeback_lock);
170 /* see if this page is already pending a writeback under a suitable key
171 * - if so we can just join onto that one */
172 wb = (struct afs_writeback *) page_private(page);
173 if (wb) {
174 if (wb->key == key && wb->state == AFS_WBACK_PENDING)
175 goto subsume_in_current_wb;
176 goto flush_conflicting_wb;
179 if (index > 0) {
180 /* see if we can find an already pending writeback that we can
181 * append this page to */
182 list_for_each_entry(wb, &vnode->writebacks, link) {
183 if (wb->last == index - 1 && wb->key == key &&
184 wb->state == AFS_WBACK_PENDING)
185 goto append_to_previous_wb;
189 list_add_tail(&candidate->link, &vnode->writebacks);
190 candidate->key = key_get(key);
191 spin_unlock(&vnode->writeback_lock);
192 SetPagePrivate(page);
193 set_page_private(page, (unsigned long) candidate);
194 _leave(" = 0 [new]");
195 return 0;
197 subsume_in_current_wb:
198 _debug("subsume");
199 ASSERTRANGE(wb->first, <=, index, <=, wb->last);
200 if (index == wb->first && from < wb->offset_first)
201 wb->offset_first = from;
202 if (index == wb->last && to > wb->to_last)
203 wb->to_last = to;
204 spin_unlock(&vnode->writeback_lock);
205 kfree(candidate);
206 _leave(" = 0 [sub]");
207 return 0;
209 append_to_previous_wb:
210 _debug("append into %lx-%lx", wb->first, wb->last);
211 wb->usage++;
212 wb->last++;
213 wb->to_last = to;
214 spin_unlock(&vnode->writeback_lock);
215 SetPagePrivate(page);
216 set_page_private(page, (unsigned long) wb);
217 kfree(candidate);
218 _leave(" = 0 [app]");
219 return 0;
221 /* the page is currently bound to another context, so if it's dirty we
222 * need to flush it before we can use the new context */
223 flush_conflicting_wb:
224 _debug("flush conflict");
225 if (wb->state == AFS_WBACK_PENDING)
226 wb->state = AFS_WBACK_CONFLICTING;
227 spin_unlock(&vnode->writeback_lock);
228 if (PageDirty(page)) {
229 ret = afs_write_back_from_locked_page(wb, page);
230 if (ret < 0) {
231 afs_put_writeback(candidate);
232 _leave(" = %d", ret);
233 return ret;
237 /* the page holds a ref on the writeback record */
238 afs_put_writeback(wb);
239 set_page_private(page, 0);
240 ClearPagePrivate(page);
241 goto try_again;
245 * finalise part of a write to a page
247 int afs_write_end(struct file *file, struct address_space *mapping,
248 loff_t pos, unsigned len, unsigned copied,
249 struct page *page, void *fsdata)
251 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
252 loff_t i_size, maybe_i_size;
254 _enter("{%x:%u},{%lx}",
255 vnode->fid.vid, vnode->fid.vnode, page->index);
257 maybe_i_size = pos + copied;
259 i_size = i_size_read(&vnode->vfs_inode);
260 if (maybe_i_size > i_size) {
261 spin_lock(&vnode->writeback_lock);
262 i_size = i_size_read(&vnode->vfs_inode);
263 if (maybe_i_size > i_size)
264 i_size_write(&vnode->vfs_inode, maybe_i_size);
265 spin_unlock(&vnode->writeback_lock);
268 set_page_dirty(page);
269 if (PageDirty(page))
270 _debug("dirtied");
271 unlock_page(page);
272 page_cache_release(page);
274 return copied;
278 * kill all the pages in the given range
280 static void afs_kill_pages(struct afs_vnode *vnode, bool error,
281 pgoff_t first, pgoff_t last)
283 struct pagevec pv;
284 unsigned count, loop;
286 _enter("{%x:%u},%lx-%lx",
287 vnode->fid.vid, vnode->fid.vnode, first, last);
289 pagevec_init(&pv, 0);
291 do {
292 _debug("kill %lx-%lx", first, last);
294 count = last - first + 1;
295 if (count > PAGEVEC_SIZE)
296 count = PAGEVEC_SIZE;
297 pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping,
298 first, count, pv.pages);
299 ASSERTCMP(pv.nr, ==, count);
301 for (loop = 0; loop < count; loop++) {
302 struct page *page = pv.pages[loop];
303 ClearPageUptodate(page);
304 if (error)
305 SetPageError(page);
306 if (PageWriteback(page))
307 end_page_writeback(page);
308 if (page->index >= first)
309 first = page->index + 1;
312 __pagevec_release(&pv);
313 } while (first < last);
315 _leave("");
319 * synchronously write back the locked page and any subsequent non-locked dirty
320 * pages also covered by the same writeback record
322 static int afs_write_back_from_locked_page(struct afs_writeback *wb,
323 struct page *primary_page)
325 struct page *pages[8], *page;
326 unsigned long count;
327 unsigned n, offset, to;
328 pgoff_t start, first, last;
329 int loop, ret;
331 _enter(",%lx", primary_page->index);
333 count = 1;
334 if (!clear_page_dirty_for_io(primary_page))
335 BUG();
336 if (test_set_page_writeback(primary_page))
337 BUG();
339 /* find all consecutive lockable dirty pages, stopping when we find a
340 * page that is not immediately lockable, is not dirty or is missing,
341 * or we reach the end of the range */
342 start = primary_page->index;
343 if (start >= wb->last)
344 goto no_more;
345 start++;
346 do {
347 _debug("more %lx [%lx]", start, count);
348 n = wb->last - start + 1;
349 if (n > ARRAY_SIZE(pages))
350 n = ARRAY_SIZE(pages);
351 n = find_get_pages_contig(wb->vnode->vfs_inode.i_mapping,
352 start, n, pages);
353 _debug("fgpc %u", n);
354 if (n == 0)
355 goto no_more;
356 if (pages[0]->index != start) {
357 do {
358 put_page(pages[--n]);
359 } while (n > 0);
360 goto no_more;
363 for (loop = 0; loop < n; loop++) {
364 page = pages[loop];
365 if (page->index > wb->last)
366 break;
367 if (!trylock_page(page))
368 break;
369 if (!PageDirty(page) ||
370 page_private(page) != (unsigned long) wb) {
371 unlock_page(page);
372 break;
374 if (!clear_page_dirty_for_io(page))
375 BUG();
376 if (test_set_page_writeback(page))
377 BUG();
378 unlock_page(page);
379 put_page(page);
381 count += loop;
382 if (loop < n) {
383 for (; loop < n; loop++)
384 put_page(pages[loop]);
385 goto no_more;
388 start += loop;
389 } while (start <= wb->last && count < 65536);
391 no_more:
392 /* we now have a contiguous set of dirty pages, each with writeback set
393 * and the dirty mark cleared; the first page is locked and must remain
394 * so, all the rest are unlocked */
395 first = primary_page->index;
396 last = first + count - 1;
398 offset = (first == wb->first) ? wb->offset_first : 0;
399 to = (last == wb->last) ? wb->to_last : PAGE_SIZE;
401 _debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to);
403 ret = afs_vnode_store_data(wb, first, last, offset, to);
404 if (ret < 0) {
405 switch (ret) {
406 case -EDQUOT:
407 case -ENOSPC:
408 set_bit(AS_ENOSPC,
409 &wb->vnode->vfs_inode.i_mapping->flags);
410 break;
411 case -EROFS:
412 case -EIO:
413 case -EREMOTEIO:
414 case -EFBIG:
415 case -ENOENT:
416 case -ENOMEDIUM:
417 case -ENXIO:
418 afs_kill_pages(wb->vnode, true, first, last);
419 set_bit(AS_EIO, &wb->vnode->vfs_inode.i_mapping->flags);
420 break;
421 case -EACCES:
422 case -EPERM:
423 case -ENOKEY:
424 case -EKEYEXPIRED:
425 case -EKEYREJECTED:
426 case -EKEYREVOKED:
427 afs_kill_pages(wb->vnode, false, first, last);
428 break;
429 default:
430 break;
432 } else {
433 ret = count;
436 _leave(" = %d", ret);
437 return ret;
441 * write a page back to the server
442 * - the caller locked the page for us
444 int afs_writepage(struct page *page, struct writeback_control *wbc)
446 struct afs_writeback *wb;
447 int ret;
449 _enter("{%lx},", page->index);
451 wb = (struct afs_writeback *) page_private(page);
452 ASSERT(wb != NULL);
454 ret = afs_write_back_from_locked_page(wb, page);
455 unlock_page(page);
456 if (ret < 0) {
457 _leave(" = %d", ret);
458 return 0;
461 wbc->nr_to_write -= ret;
463 _leave(" = 0");
464 return 0;
468 * write a region of pages back to the server
470 static int afs_writepages_region(struct address_space *mapping,
471 struct writeback_control *wbc,
472 pgoff_t index, pgoff_t end, pgoff_t *_next)
474 struct afs_writeback *wb;
475 struct page *page;
476 int ret, n;
478 _enter(",,%lx,%lx,", index, end);
480 do {
481 n = find_get_pages_tag(mapping, &index, PAGECACHE_TAG_DIRTY,
482 1, &page);
483 if (!n)
484 break;
486 _debug("wback %lx", page->index);
488 if (page->index > end) {
489 *_next = index;
490 page_cache_release(page);
491 _leave(" = 0 [%lx]", *_next);
492 return 0;
495 /* at this point we hold neither mapping->tree_lock nor lock on
496 * the page itself: the page may be truncated or invalidated
497 * (changing page->mapping to NULL), or even swizzled back from
498 * swapper_space to tmpfs file mapping
500 lock_page(page);
502 if (page->mapping != mapping) {
503 unlock_page(page);
504 page_cache_release(page);
505 continue;
508 if (wbc->sync_mode != WB_SYNC_NONE)
509 wait_on_page_writeback(page);
511 if (PageWriteback(page) || !PageDirty(page)) {
512 unlock_page(page);
513 put_page(page);
514 continue;
517 wb = (struct afs_writeback *) page_private(page);
518 ASSERT(wb != NULL);
520 spin_lock(&wb->vnode->writeback_lock);
521 wb->state = AFS_WBACK_WRITING;
522 spin_unlock(&wb->vnode->writeback_lock);
524 ret = afs_write_back_from_locked_page(wb, page);
525 unlock_page(page);
526 page_cache_release(page);
527 if (ret < 0) {
528 _leave(" = %d", ret);
529 return ret;
532 wbc->nr_to_write -= ret;
534 cond_resched();
535 } while (index < end && wbc->nr_to_write > 0);
537 *_next = index;
538 _leave(" = 0 [%lx]", *_next);
539 return 0;
543 * write some of the pending data back to the server
545 int afs_writepages(struct address_space *mapping,
546 struct writeback_control *wbc)
548 pgoff_t start, end, next;
549 int ret;
551 _enter("");
553 if (wbc->range_cyclic) {
554 start = mapping->writeback_index;
555 end = -1;
556 ret = afs_writepages_region(mapping, wbc, start, end, &next);
557 if (start > 0 && wbc->nr_to_write > 0 && ret == 0)
558 ret = afs_writepages_region(mapping, wbc, 0, start,
559 &next);
560 mapping->writeback_index = next;
561 } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
562 end = (pgoff_t)(LLONG_MAX >> PAGE_CACHE_SHIFT);
563 ret = afs_writepages_region(mapping, wbc, 0, end, &next);
564 if (wbc->nr_to_write > 0)
565 mapping->writeback_index = next;
566 } else {
567 start = wbc->range_start >> PAGE_CACHE_SHIFT;
568 end = wbc->range_end >> PAGE_CACHE_SHIFT;
569 ret = afs_writepages_region(mapping, wbc, start, end, &next);
572 _leave(" = %d", ret);
573 return ret;
577 * completion of write to server
579 void afs_pages_written_back(struct afs_vnode *vnode, struct afs_call *call)
581 struct afs_writeback *wb = call->wb;
582 struct pagevec pv;
583 unsigned count, loop;
584 pgoff_t first = call->first, last = call->last;
585 bool free_wb;
587 _enter("{%x:%u},{%lx-%lx}",
588 vnode->fid.vid, vnode->fid.vnode, first, last);
590 ASSERT(wb != NULL);
592 pagevec_init(&pv, 0);
594 do {
595 _debug("done %lx-%lx", first, last);
597 count = last - first + 1;
598 if (count > PAGEVEC_SIZE)
599 count = PAGEVEC_SIZE;
600 pv.nr = find_get_pages_contig(call->mapping, first, count,
601 pv.pages);
602 ASSERTCMP(pv.nr, ==, count);
604 spin_lock(&vnode->writeback_lock);
605 for (loop = 0; loop < count; loop++) {
606 struct page *page = pv.pages[loop];
607 end_page_writeback(page);
608 if (page_private(page) == (unsigned long) wb) {
609 set_page_private(page, 0);
610 ClearPagePrivate(page);
611 wb->usage--;
614 free_wb = false;
615 if (wb->usage == 0) {
616 afs_unlink_writeback(wb);
617 free_wb = true;
619 spin_unlock(&vnode->writeback_lock);
620 first += count;
621 if (free_wb) {
622 afs_free_writeback(wb);
623 wb = NULL;
626 __pagevec_release(&pv);
627 } while (first <= last);
629 _leave("");
633 * write to an AFS file
635 ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from)
637 struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
638 ssize_t result;
639 size_t count = iov_iter_count(from);
641 _enter("{%x.%u},{%zu},",
642 vnode->fid.vid, vnode->fid.vnode, count);
644 if (IS_SWAPFILE(&vnode->vfs_inode)) {
645 printk(KERN_INFO
646 "AFS: Attempt to write to active swap file!\n");
647 return -EBUSY;
650 if (!count)
651 return 0;
653 result = generic_file_write_iter(iocb, from);
654 if (IS_ERR_VALUE(result)) {
655 _leave(" = %zd", result);
656 return result;
659 _leave(" = %zd", result);
660 return result;
664 * flush the vnode to the fileserver
666 int afs_writeback_all(struct afs_vnode *vnode)
668 struct address_space *mapping = vnode->vfs_inode.i_mapping;
669 struct writeback_control wbc = {
670 .sync_mode = WB_SYNC_ALL,
671 .nr_to_write = LONG_MAX,
672 .range_cyclic = 1,
674 int ret;
676 _enter("");
678 ret = mapping->a_ops->writepages(mapping, &wbc);
679 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
681 _leave(" = %d", ret);
682 return ret;
686 * flush any dirty pages for this process, and check for write errors.
687 * - the return status from this call provides a reliable indication of
688 * whether any write errors occurred for this process.
690 int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
692 struct inode *inode = file_inode(file);
693 struct afs_writeback *wb, *xwb;
694 struct afs_vnode *vnode = AFS_FS_I(inode);
695 int ret;
697 _enter("{%x:%u},{n=%pD},%d",
698 vnode->fid.vid, vnode->fid.vnode, file,
699 datasync);
701 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
702 if (ret)
703 return ret;
704 mutex_lock(&inode->i_mutex);
706 /* use a writeback record as a marker in the queue - when this reaches
707 * the front of the queue, all the outstanding writes are either
708 * completed or rejected */
709 wb = kzalloc(sizeof(*wb), GFP_KERNEL);
710 if (!wb) {
711 ret = -ENOMEM;
712 goto out;
714 wb->vnode = vnode;
715 wb->first = 0;
716 wb->last = -1;
717 wb->offset_first = 0;
718 wb->to_last = PAGE_SIZE;
719 wb->usage = 1;
720 wb->state = AFS_WBACK_SYNCING;
721 init_waitqueue_head(&wb->waitq);
723 spin_lock(&vnode->writeback_lock);
724 list_for_each_entry(xwb, &vnode->writebacks, link) {
725 if (xwb->state == AFS_WBACK_PENDING)
726 xwb->state = AFS_WBACK_CONFLICTING;
728 list_add_tail(&wb->link, &vnode->writebacks);
729 spin_unlock(&vnode->writeback_lock);
731 /* push all the outstanding writebacks to the server */
732 ret = afs_writeback_all(vnode);
733 if (ret < 0) {
734 afs_put_writeback(wb);
735 _leave(" = %d [wb]", ret);
736 goto out;
739 /* wait for the preceding writes to actually complete */
740 ret = wait_event_interruptible(wb->waitq,
741 wb->state == AFS_WBACK_COMPLETE ||
742 vnode->writebacks.next == &wb->link);
743 afs_put_writeback(wb);
744 _leave(" = %d", ret);
745 out:
746 mutex_unlock(&inode->i_mutex);
747 return ret;
751 * Flush out all outstanding writes on a file opened for writing when it is
752 * closed.
754 int afs_flush(struct file *file, fl_owner_t id)
756 _enter("");
758 if ((file->f_mode & FMODE_WRITE) == 0)
759 return 0;
761 return vfs_fsync(file, 0);
765 * notification that a previously read-only page is about to become writable
766 * - if it returns an error, the caller will deliver a bus error signal
768 int afs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
770 struct afs_vnode *vnode = AFS_FS_I(vma->vm_file->f_mapping->host);
772 _enter("{{%x:%u}},{%lx}",
773 vnode->fid.vid, vnode->fid.vnode, page->index);
775 /* wait for the page to be written to the cache before we allow it to
776 * be modified */
777 #ifdef CONFIG_AFS_FSCACHE
778 fscache_wait_on_page_write(vnode->cache, page);
779 #endif
781 _leave(" = 0");
782 return 0;