aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
authorDavid Howells <dhowells@redhat.com>2007-05-09 05:33:46 -0400
committerLinus Torvalds <torvalds@woody.linux-foundation.org>2007-05-09 15:30:50 -0400
commit31143d5d515ece617ffccb7df5ff75e4d1dfa120 (patch)
treedb28c26930f6a26db3e85da90f6668061425463a /fs
parent416351f28d2b31d15ff73e9aff699b2163704c95 (diff)
AFS: implement basic file write support
Implement support for writing to regular AFS files, including: (1) write (2) truncate (3) fsync, fdatasync (4) chmod, chown, chgrp, utime. AFS writeback attempts to batch writes into as chunks as large as it can manage up to the point that it writes back 65535 pages in one chunk or it meets a locked page. Furthermore, if a page has been written to using a particular key, then should another write to that page use some other key, the first write will be flushed before the second is allowed to take place. If the first write fails due to a security error, then the page will be scrapped and reread before the second write takes place. If a page is dirty and the callback on it is broken by the server, then the dirty data is not discarded (same behaviour as NFS). Shared-writable mappings are not supported by this patch. [akpm@linux-foundation.org: fix a bunch of warnings] Signed-off-by: David Howells <dhowells@redhat.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'fs')
-rw-r--r--fs/afs/Makefile3
-rw-r--r--fs/afs/afs_fs.h2
-rw-r--r--fs/afs/dir.c1
-rw-r--r--fs/afs/file.c32
-rw-r--r--fs/afs/fsclient.c332
-rw-r--r--fs/afs/inode.c47
-rw-r--r--fs/afs/internal.h66
-rw-r--r--fs/afs/rxrpc.c80
-rw-r--r--fs/afs/super.c5
-rw-r--r--fs/afs/vnode.c107
-rw-r--r--fs/afs/write.c835
11 files changed, 1483 insertions, 27 deletions
diff --git a/fs/afs/Makefile b/fs/afs/Makefile
index cf83e5d63512..73ce561f3ea0 100644
--- a/fs/afs/Makefile
+++ b/fs/afs/Makefile
@@ -22,6 +22,7 @@ kafs-objs := \
22 vlclient.o \ 22 vlclient.o \
23 vlocation.o \ 23 vlocation.o \
24 vnode.o \ 24 vnode.o \
25 volume.o 25 volume.o \
26 write.o
26 27
27obj-$(CONFIG_AFS_FS) := kafs.o 28obj-$(CONFIG_AFS_FS) := kafs.o
diff --git a/fs/afs/afs_fs.h b/fs/afs/afs_fs.h
index 89e0d1650a72..2198006d2d03 100644
--- a/fs/afs/afs_fs.h
+++ b/fs/afs/afs_fs.h
@@ -18,6 +18,8 @@
18enum AFS_FS_Operations { 18enum AFS_FS_Operations {
19 FSFETCHDATA = 130, /* AFS Fetch file data */ 19 FSFETCHDATA = 130, /* AFS Fetch file data */
20 FSFETCHSTATUS = 132, /* AFS Fetch file status */ 20 FSFETCHSTATUS = 132, /* AFS Fetch file status */
21 FSSTOREDATA = 133, /* AFS Store file data */
22 FSSTORESTATUS = 135, /* AFS Store file status */
21 FSREMOVEFILE = 136, /* AFS Remove a file */ 23 FSREMOVEFILE = 136, /* AFS Remove a file */
22 FSCREATEFILE = 137, /* AFS Create a file */ 24 FSCREATEFILE = 137, /* AFS Create a file */
23 FSRENAME = 138, /* AFS Rename or move a file or directory */ 25 FSRENAME = 138, /* AFS Rename or move a file or directory */
diff --git a/fs/afs/dir.c b/fs/afs/dir.c
index 51f177af5dd6..2fb31276196b 100644
--- a/fs/afs/dir.c
+++ b/fs/afs/dir.c
@@ -56,6 +56,7 @@ const struct inode_operations afs_dir_inode_operations = {
56 .rename = afs_rename, 56 .rename = afs_rename,
57 .permission = afs_permission, 57 .permission = afs_permission,
58 .getattr = afs_getattr, 58 .getattr = afs_getattr,
59 .setattr = afs_setattr,
59}; 60};
60 61
61static struct dentry_operations afs_fs_dentry_operations = { 62static struct dentry_operations afs_fs_dentry_operations = {
diff --git a/fs/afs/file.c b/fs/afs/file.c
index 3eb3fc7b36be..3e25795e5a42 100644
--- a/fs/afs/file.c
+++ b/fs/afs/file.c
@@ -15,32 +15,43 @@
15#include <linux/slab.h> 15#include <linux/slab.h>
16#include <linux/fs.h> 16#include <linux/fs.h>
17#include <linux/pagemap.h> 17#include <linux/pagemap.h>
18#include <linux/writeback.h>
18#include "internal.h" 19#include "internal.h"
19 20
20static int afs_readpage(struct file *file, struct page *page); 21static int afs_readpage(struct file *file, struct page *page);
21static void afs_invalidatepage(struct page *page, unsigned long offset); 22static void afs_invalidatepage(struct page *page, unsigned long offset);
22static int afs_releasepage(struct page *page, gfp_t gfp_flags); 23static int afs_releasepage(struct page *page, gfp_t gfp_flags);
24static int afs_launder_page(struct page *page);
23 25
24const struct file_operations afs_file_operations = { 26const struct file_operations afs_file_operations = {
25 .open = afs_open, 27 .open = afs_open,
26 .release = afs_release, 28 .release = afs_release,
27 .llseek = generic_file_llseek, 29 .llseek = generic_file_llseek,
28 .read = do_sync_read, 30 .read = do_sync_read,
31 .write = do_sync_write,
29 .aio_read = generic_file_aio_read, 32 .aio_read = generic_file_aio_read,
33 .aio_write = afs_file_write,
30 .mmap = generic_file_readonly_mmap, 34 .mmap = generic_file_readonly_mmap,
31 .sendfile = generic_file_sendfile, 35 .sendfile = generic_file_sendfile,
36 .fsync = afs_fsync,
32}; 37};
33 38
34const struct inode_operations afs_file_inode_operations = { 39const struct inode_operations afs_file_inode_operations = {
35 .getattr = afs_getattr, 40 .getattr = afs_getattr,
41 .setattr = afs_setattr,
36 .permission = afs_permission, 42 .permission = afs_permission,
37}; 43};
38 44
39const struct address_space_operations afs_fs_aops = { 45const struct address_space_operations afs_fs_aops = {
40 .readpage = afs_readpage, 46 .readpage = afs_readpage,
41 .set_page_dirty = __set_page_dirty_nobuffers, 47 .set_page_dirty = afs_set_page_dirty,
48 .launder_page = afs_launder_page,
42 .releasepage = afs_releasepage, 49 .releasepage = afs_releasepage,
43 .invalidatepage = afs_invalidatepage, 50 .invalidatepage = afs_invalidatepage,
51 .prepare_write = afs_prepare_write,
52 .commit_write = afs_commit_write,
53 .writepage = afs_writepage,
54 .writepages = afs_writepages,
44}; 55};
45 56
46/* 57/*
@@ -230,11 +241,6 @@ static void afs_invalidatepage(struct page *page, unsigned long offset)
230 BUG_ON(!PageLocked(page)); 241 BUG_ON(!PageLocked(page));
231 242
232 if (PagePrivate(page)) { 243 if (PagePrivate(page)) {
233#ifdef AFS_CACHING_SUPPORT
234 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
235 cachefs_uncache_page(vnode->cache,page);
236#endif
237
238 /* We release buffers only if the entire page is being 244 /* We release buffers only if the entire page is being
239 * invalidated. 245 * invalidated.
240 * The get_block cached value has been unconditionally 246 * The get_block cached value has been unconditionally
@@ -255,19 +261,33 @@ static void afs_invalidatepage(struct page *page, unsigned long offset)
255} 261}
256 262
257/* 263/*
264 * write back a dirty page
265 */
266static int afs_launder_page(struct page *page)
267{
268 _enter("{%lu}", page->index);
269
270 return 0;
271}
272
273/*
258 * release a page and cleanup its private data 274 * release a page and cleanup its private data
259 */ 275 */
260static int afs_releasepage(struct page *page, gfp_t gfp_flags) 276static int afs_releasepage(struct page *page, gfp_t gfp_flags)
261{ 277{
262 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host); 278 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
279 struct afs_writeback *wb;
263 280
264 _enter("{{%x:%u}[%lu],%lx},%x", 281 _enter("{{%x:%u}[%lu],%lx},%x",
265 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags, 282 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
266 gfp_flags); 283 gfp_flags);
267 284
268 if (PagePrivate(page)) { 285 if (PagePrivate(page)) {
286 wb = (struct afs_writeback *) page_private(page);
287 ASSERT(wb != NULL);
269 set_page_private(page, 0); 288 set_page_private(page, 0);
270 ClearPagePrivate(page); 289 ClearPagePrivate(page);
290 afs_put_writeback(wb);
271 } 291 }
272 292
273 _leave(" = 0"); 293 _leave(" = 0");
diff --git a/fs/afs/fsclient.c b/fs/afs/fsclient.c
index 1e65fee36413..025b1903d9e1 100644
--- a/fs/afs/fsclient.c
+++ b/fs/afs/fsclient.c
@@ -33,8 +33,10 @@ static void xdr_decode_AFSFid(const __be32 **_bp, struct afs_fid *fid)
33 */ 33 */
34static void xdr_decode_AFSFetchStatus(const __be32 **_bp, 34static void xdr_decode_AFSFetchStatus(const __be32 **_bp,
35 struct afs_file_status *status, 35 struct afs_file_status *status,
36 struct afs_vnode *vnode) 36 struct afs_vnode *vnode,
37 afs_dataversion_t *store_version)
37{ 38{
39 afs_dataversion_t expected_version;
38 const __be32 *bp = *_bp; 40 const __be32 *bp = *_bp;
39 umode_t mode; 41 umode_t mode;
40 u64 data_version, size; 42 u64 data_version, size;
@@ -101,7 +103,11 @@ static void xdr_decode_AFSFetchStatus(const __be32 **_bp,
101 vnode->vfs_inode.i_atime = vnode->vfs_inode.i_ctime; 103 vnode->vfs_inode.i_atime = vnode->vfs_inode.i_ctime;
102 } 104 }
103 105
104 if (status->data_version != data_version) { 106 expected_version = status->data_version;
107 if (store_version)
108 expected_version = *store_version;
109
110 if (expected_version != data_version) {
105 status->data_version = data_version; 111 status->data_version = data_version;
106 if (vnode && !test_bit(AFS_VNODE_UNSET, &vnode->flags)) { 112 if (vnode && !test_bit(AFS_VNODE_UNSET, &vnode->flags)) {
107 _debug("vnode modified %llx on {%x:%u}", 113 _debug("vnode modified %llx on {%x:%u}",
@@ -110,6 +116,8 @@ static void xdr_decode_AFSFetchStatus(const __be32 **_bp,
110 set_bit(AFS_VNODE_MODIFIED, &vnode->flags); 116 set_bit(AFS_VNODE_MODIFIED, &vnode->flags);
111 set_bit(AFS_VNODE_ZAP_DATA, &vnode->flags); 117 set_bit(AFS_VNODE_ZAP_DATA, &vnode->flags);
112 } 118 }
119 } else if (store_version) {
120 status->data_version = data_version;
113 } 121 }
114} 122}
115 123
@@ -156,6 +164,44 @@ static void xdr_decode_AFSVolSync(const __be32 **_bp,
156} 164}
157 165
158/* 166/*
167 * encode the requested attributes into an AFSStoreStatus block
168 */
169static void xdr_encode_AFS_StoreStatus(__be32 **_bp, struct iattr *attr)
170{
171 __be32 *bp = *_bp;
172 u32 mask = 0, mtime = 0, owner = 0, group = 0, mode = 0;
173
174 mask = 0;
175 if (attr->ia_valid & ATTR_MTIME) {
176 mask |= AFS_SET_MTIME;
177 mtime = attr->ia_mtime.tv_sec;
178 }
179
180 if (attr->ia_valid & ATTR_UID) {
181 mask |= AFS_SET_OWNER;
182 owner = attr->ia_uid;
183 }
184
185 if (attr->ia_valid & ATTR_GID) {
186 mask |= AFS_SET_GROUP;
187 group = attr->ia_gid;
188 }
189
190 if (attr->ia_valid & ATTR_MODE) {
191 mask |= AFS_SET_MODE;
192 mode = attr->ia_mode & S_IALLUGO;
193 }
194
195 *bp++ = htonl(mask);
196 *bp++ = htonl(mtime);
197 *bp++ = htonl(owner);
198 *bp++ = htonl(group);
199 *bp++ = htonl(mode);
200 *bp++ = 0; /* segment size */
201 *_bp = bp;
202}
203
204/*
159 * deliver reply data to an FS.FetchStatus 205 * deliver reply data to an FS.FetchStatus
160 */ 206 */
161static int afs_deliver_fs_fetch_status(struct afs_call *call, 207static int afs_deliver_fs_fetch_status(struct afs_call *call,
@@ -175,7 +221,7 @@ static int afs_deliver_fs_fetch_status(struct afs_call *call,
175 221
176 /* unmarshall the reply once we've received all of it */ 222 /* unmarshall the reply once we've received all of it */
177 bp = call->buffer; 223 bp = call->buffer;
178 xdr_decode_AFSFetchStatus(&bp, &vnode->status, vnode); 224 xdr_decode_AFSFetchStatus(&bp, &vnode->status, vnode, NULL);
179 xdr_decode_AFSCallBack(&bp, vnode); 225 xdr_decode_AFSCallBack(&bp, vnode);
180 if (call->reply2) 226 if (call->reply2)
181 xdr_decode_AFSVolSync(&bp, call->reply2); 227 xdr_decode_AFSVolSync(&bp, call->reply2);
@@ -295,7 +341,7 @@ static int afs_deliver_fs_fetch_data(struct afs_call *call,
295 } 341 }
296 342
297 bp = call->buffer; 343 bp = call->buffer;
298 xdr_decode_AFSFetchStatus(&bp, &vnode->status, vnode); 344 xdr_decode_AFSFetchStatus(&bp, &vnode->status, vnode, NULL);
299 xdr_decode_AFSCallBack(&bp, vnode); 345 xdr_decode_AFSCallBack(&bp, vnode);
300 if (call->reply2) 346 if (call->reply2)
301 xdr_decode_AFSVolSync(&bp, call->reply2); 347 xdr_decode_AFSVolSync(&bp, call->reply2);
@@ -479,8 +525,8 @@ static int afs_deliver_fs_create_vnode(struct afs_call *call,
479 /* unmarshall the reply once we've received all of it */ 525 /* unmarshall the reply once we've received all of it */
480 bp = call->buffer; 526 bp = call->buffer;
481 xdr_decode_AFSFid(&bp, call->reply2); 527 xdr_decode_AFSFid(&bp, call->reply2);
482 xdr_decode_AFSFetchStatus(&bp, call->reply3, NULL); 528 xdr_decode_AFSFetchStatus(&bp, call->reply3, NULL, NULL);
483 xdr_decode_AFSFetchStatus(&bp, &vnode->status, vnode); 529 xdr_decode_AFSFetchStatus(&bp, &vnode->status, vnode, NULL);
484 xdr_decode_AFSCallBack_raw(&bp, call->reply4); 530 xdr_decode_AFSCallBack_raw(&bp, call->reply4);
485 /* xdr_decode_AFSVolSync(&bp, call->replyX); */ 531 /* xdr_decode_AFSVolSync(&bp, call->replyX); */
486 532
@@ -577,7 +623,7 @@ static int afs_deliver_fs_remove(struct afs_call *call,
577 623
578 /* unmarshall the reply once we've received all of it */ 624 /* unmarshall the reply once we've received all of it */
579 bp = call->buffer; 625 bp = call->buffer;
580 xdr_decode_AFSFetchStatus(&bp, &vnode->status, vnode); 626 xdr_decode_AFSFetchStatus(&bp, &vnode->status, vnode, NULL);
581 /* xdr_decode_AFSVolSync(&bp, call->replyX); */ 627 /* xdr_decode_AFSVolSync(&bp, call->replyX); */
582 628
583 _leave(" = 0 [done]"); 629 _leave(" = 0 [done]");
@@ -660,8 +706,8 @@ static int afs_deliver_fs_link(struct afs_call *call,
660 706
661 /* unmarshall the reply once we've received all of it */ 707 /* unmarshall the reply once we've received all of it */
662 bp = call->buffer; 708 bp = call->buffer;
663 xdr_decode_AFSFetchStatus(&bp, &vnode->status, vnode); 709 xdr_decode_AFSFetchStatus(&bp, &vnode->status, vnode, NULL);
664 xdr_decode_AFSFetchStatus(&bp, &dvnode->status, dvnode); 710 xdr_decode_AFSFetchStatus(&bp, &dvnode->status, dvnode, NULL);
665 /* xdr_decode_AFSVolSync(&bp, call->replyX); */ 711 /* xdr_decode_AFSVolSync(&bp, call->replyX); */
666 712
667 _leave(" = 0 [done]"); 713 _leave(" = 0 [done]");
@@ -749,8 +795,8 @@ static int afs_deliver_fs_symlink(struct afs_call *call,
749 /* unmarshall the reply once we've received all of it */ 795 /* unmarshall the reply once we've received all of it */
750 bp = call->buffer; 796 bp = call->buffer;
751 xdr_decode_AFSFid(&bp, call->reply2); 797 xdr_decode_AFSFid(&bp, call->reply2);
752 xdr_decode_AFSFetchStatus(&bp, call->reply3, NULL); 798 xdr_decode_AFSFetchStatus(&bp, call->reply3, NULL, NULL);
753 xdr_decode_AFSFetchStatus(&bp, &vnode->status, vnode); 799 xdr_decode_AFSFetchStatus(&bp, &vnode->status, vnode, NULL);
754 /* xdr_decode_AFSVolSync(&bp, call->replyX); */ 800 /* xdr_decode_AFSVolSync(&bp, call->replyX); */
755 801
756 _leave(" = 0 [done]"); 802 _leave(" = 0 [done]");
@@ -855,9 +901,10 @@ static int afs_deliver_fs_rename(struct afs_call *call,
855 901
856 /* unmarshall the reply once we've received all of it */ 902 /* unmarshall the reply once we've received all of it */
857 bp = call->buffer; 903 bp = call->buffer;
858 xdr_decode_AFSFetchStatus(&bp, &orig_dvnode->status, orig_dvnode); 904 xdr_decode_AFSFetchStatus(&bp, &orig_dvnode->status, orig_dvnode, NULL);
859 if (new_dvnode != orig_dvnode) 905 if (new_dvnode != orig_dvnode)
860 xdr_decode_AFSFetchStatus(&bp, &new_dvnode->status, new_dvnode); 906 xdr_decode_AFSFetchStatus(&bp, &new_dvnode->status, new_dvnode,
907 NULL);
861 /* xdr_decode_AFSVolSync(&bp, call->replyX); */ 908 /* xdr_decode_AFSVolSync(&bp, call->replyX); */
862 909
863 _leave(" = 0 [done]"); 910 _leave(" = 0 [done]");
@@ -939,3 +986,262 @@ int afs_fs_rename(struct afs_server *server,
939 986
940 return afs_make_call(&server->addr, call, GFP_NOFS, wait_mode); 987 return afs_make_call(&server->addr, call, GFP_NOFS, wait_mode);
941} 988}
989
990/*
991 * deliver reply data to an FS.StoreData
992 */
993static int afs_deliver_fs_store_data(struct afs_call *call,
994 struct sk_buff *skb, bool last)
995{
996 struct afs_vnode *vnode = call->reply;
997 const __be32 *bp;
998
999 _enter(",,%u", last);
1000
1001 afs_transfer_reply(call, skb);
1002 if (!last) {
1003 _leave(" = 0 [more]");
1004 return 0;
1005 }
1006
1007 if (call->reply_size != call->reply_max) {
1008 _leave(" = -EBADMSG [%u != %u]",
1009 call->reply_size, call->reply_max);
1010 return -EBADMSG;
1011 }
1012
1013 /* unmarshall the reply once we've received all of it */
1014 bp = call->buffer;
1015 xdr_decode_AFSFetchStatus(&bp, &vnode->status, vnode,
1016 &call->store_version);
1017 /* xdr_decode_AFSVolSync(&bp, call->replyX); */
1018
1019 afs_pages_written_back(vnode, call);
1020
1021 _leave(" = 0 [done]");
1022 return 0;
1023}
1024
1025/*
1026 * FS.StoreData operation type
1027 */
1028static const struct afs_call_type afs_RXFSStoreData = {
1029 .name = "FS.StoreData",
1030 .deliver = afs_deliver_fs_store_data,
1031 .abort_to_error = afs_abort_to_error,
1032 .destructor = afs_flat_call_destructor,
1033};
1034
1035/*
1036 * store a set of pages
1037 */
1038int afs_fs_store_data(struct afs_server *server, struct afs_writeback *wb,
1039 pgoff_t first, pgoff_t last,
1040 unsigned offset, unsigned to,
1041 const struct afs_wait_mode *wait_mode)
1042{
1043 struct afs_vnode *vnode = wb->vnode;
1044 struct afs_call *call;
1045 loff_t size, pos, i_size;
1046 __be32 *bp;
1047
1048 _enter(",%x,{%x:%u},,",
1049 key_serial(wb->key), vnode->fid.vid, vnode->fid.vnode);
1050
1051 size = to - offset;
1052 if (first != last)
1053 size += (loff_t)(last - first) << PAGE_SHIFT;
1054 pos = (loff_t)first << PAGE_SHIFT;
1055 pos += offset;
1056
1057 i_size = i_size_read(&vnode->vfs_inode);
1058 if (pos + size > i_size)
1059 i_size = size + pos;
1060
1061 _debug("size %llx, at %llx, i_size %llx",
1062 (unsigned long long) size, (unsigned long long) pos,
1063 (unsigned long long) i_size);
1064
1065 BUG_ON(i_size > 0xffffffff); // TODO: use 64-bit store
1066
1067 call = afs_alloc_flat_call(&afs_RXFSStoreData,
1068 (4 + 6 + 3) * 4,
1069 (21 + 6) * 4);
1070 if (!call)
1071 return -ENOMEM;
1072
1073 call->wb = wb;
1074 call->key = wb->key;
1075 call->reply = vnode;
1076 call->service_id = FS_SERVICE;
1077 call->port = htons(AFS_FS_PORT);
1078 call->mapping = vnode->vfs_inode.i_mapping;
1079 call->first = first;
1080 call->last = last;
1081 call->first_offset = offset;
1082 call->last_to = to;
1083 call->send_pages = true;
1084 call->store_version = vnode->status.data_version + 1;
1085
1086 /* marshall the parameters */
1087 bp = call->request;
1088 *bp++ = htonl(FSSTOREDATA);
1089 *bp++ = htonl(vnode->fid.vid);
1090 *bp++ = htonl(vnode->fid.vnode);
1091 *bp++ = htonl(vnode->fid.unique);
1092
1093 *bp++ = 0; /* mask */
1094 *bp++ = 0; /* mtime */
1095 *bp++ = 0; /* owner */
1096 *bp++ = 0; /* group */
1097 *bp++ = 0; /* unix mode */
1098 *bp++ = 0; /* segment size */
1099
1100 *bp++ = htonl(pos);
1101 *bp++ = htonl(size);
1102 *bp++ = htonl(i_size);
1103
1104 return afs_make_call(&server->addr, call, GFP_NOFS, wait_mode);
1105}
1106
1107/*
1108 * deliver reply data to an FS.StoreStatus
1109 */
1110static int afs_deliver_fs_store_status(struct afs_call *call,
1111 struct sk_buff *skb, bool last)
1112{
1113 afs_dataversion_t *store_version;
1114 struct afs_vnode *vnode = call->reply;
1115 const __be32 *bp;
1116
1117 _enter(",,%u", last);
1118
1119 afs_transfer_reply(call, skb);
1120 if (!last) {
1121 _leave(" = 0 [more]");
1122 return 0;
1123 }
1124
1125 if (call->reply_size != call->reply_max) {
1126 _leave(" = -EBADMSG [%u != %u]",
1127 call->reply_size, call->reply_max);
1128 return -EBADMSG;
1129 }
1130
1131 /* unmarshall the reply once we've received all of it */
1132 store_version = NULL;
1133 if (call->operation_ID == FSSTOREDATA)
1134 store_version = &call->store_version;
1135
1136 bp = call->buffer;
1137 xdr_decode_AFSFetchStatus(&bp, &vnode->status, vnode, store_version);
1138 /* xdr_decode_AFSVolSync(&bp, call->replyX); */
1139
1140 _leave(" = 0 [done]");
1141 return 0;
1142}
1143
1144/*
1145 * FS.StoreStatus operation type
1146 */
1147static const struct afs_call_type afs_RXFSStoreStatus = {
1148 .name = "FS.StoreStatus",
1149 .deliver = afs_deliver_fs_store_status,
1150 .abort_to_error = afs_abort_to_error,
1151 .destructor = afs_flat_call_destructor,
1152};
1153
1154static const struct afs_call_type afs_RXFSStoreData_as_Status = {
1155 .name = "FS.StoreData",
1156 .deliver = afs_deliver_fs_store_status,
1157 .abort_to_error = afs_abort_to_error,
1158 .destructor = afs_flat_call_destructor,
1159};
1160
1161/*
1162 * set the attributes on a file, using FS.StoreData rather than FS.StoreStatus
1163 * so as to alter the file size also
1164 */
1165static int afs_fs_setattr_size(struct afs_server *server, struct key *key,
1166 struct afs_vnode *vnode, struct iattr *attr,
1167 const struct afs_wait_mode *wait_mode)
1168{
1169 struct afs_call *call;
1170 __be32 *bp;
1171
1172 _enter(",%x,{%x:%u},,",
1173 key_serial(key), vnode->fid.vid, vnode->fid.vnode);
1174
1175 ASSERT(attr->ia_valid & ATTR_SIZE);
1176 ASSERTCMP(attr->ia_size, <=, 0xffffffff); // TODO: use 64-bit store
1177
1178 call = afs_alloc_flat_call(&afs_RXFSStoreData_as_Status,
1179 (4 + 6 + 3) * 4,
1180 (21 + 6) * 4);
1181 if (!call)
1182 return -ENOMEM;
1183
1184 call->key = key;
1185 call->reply = vnode;
1186 call->service_id = FS_SERVICE;
1187 call->port = htons(AFS_FS_PORT);
1188 call->store_version = vnode->status.data_version + 1;
1189 call->operation_ID = FSSTOREDATA;
1190
1191 /* marshall the parameters */
1192 bp = call->request;
1193 *bp++ = htonl(FSSTOREDATA);
1194 *bp++ = htonl(vnode->fid.vid);
1195 *bp++ = htonl(vnode->fid.vnode);
1196 *bp++ = htonl(vnode->fid.unique);
1197
1198 xdr_encode_AFS_StoreStatus(&bp, attr);
1199
1200 *bp++ = 0; /* position of start of write */
1201 *bp++ = 0; /* size of write */
1202 *bp++ = htonl(attr->ia_size); /* new file length */
1203
1204 return afs_make_call(&server->addr, call, GFP_NOFS, wait_mode);
1205}
1206
1207/*
1208 * set the attributes on a file, using FS.StoreData if there's a change in file
1209 * size, and FS.StoreStatus otherwise
1210 */
1211int afs_fs_setattr(struct afs_server *server, struct key *key,
1212 struct afs_vnode *vnode, struct iattr *attr,
1213 const struct afs_wait_mode *wait_mode)
1214{
1215 struct afs_call *call;
1216 __be32 *bp;
1217
1218 if (attr->ia_valid & ATTR_SIZE)
1219 return afs_fs_setattr_size(server, key, vnode, attr,
1220 wait_mode);
1221
1222 _enter(",%x,{%x:%u},,",
1223 key_serial(key), vnode->fid.vid, vnode->fid.vnode);
1224
1225 call = afs_alloc_flat_call(&afs_RXFSStoreStatus,
1226 (4 + 6) * 4,
1227 (21 + 6) * 4);
1228 if (!call)
1229 return -ENOMEM;
1230
1231 call->key = key;
1232 call->reply = vnode;
1233 call->service_id = FS_SERVICE;
1234 call->port = htons(AFS_FS_PORT);
1235 call->operation_ID = FSSTORESTATUS;
1236
1237 /* marshall the parameters */
1238 bp = call->request;
1239 *bp++ = htonl(FSSTORESTATUS);
1240 *bp++ = htonl(vnode->fid.vid);
1241 *bp++ = htonl(vnode->fid.vnode);
1242 *bp++ = htonl(vnode->fid.unique);
1243
1244 xdr_encode_AFS_StoreStatus(&bp, attr);
1245
1246 return afs_make_call(&server->addr, call, GFP_NOFS, wait_mode);
1247}
diff --git a/fs/afs/inode.c b/fs/afs/inode.c
index 9c984cc42cc9..515a5d12d8fb 100644
--- a/fs/afs/inode.c
+++ b/fs/afs/inode.c
@@ -209,7 +209,7 @@ bad_inode:
209 */ 209 */
210void afs_zap_data(struct afs_vnode *vnode) 210void afs_zap_data(struct afs_vnode *vnode)
211{ 211{
212 kenter("zap data {%x:%u}", vnode->fid.vid, vnode->fid.vnode); 212 _enter("zap data {%x:%u}", vnode->fid.vid, vnode->fid.vnode);
213 213
214 /* nuke all the non-dirty pages that aren't locked, mapped or being 214 /* nuke all the non-dirty pages that aren't locked, mapped or being
215 * written back */ 215 * written back */
@@ -334,6 +334,7 @@ void afs_clear_inode(struct inode *inode)
334 vnode->server = NULL; 334 vnode->server = NULL;
335 } 335 }
336 336
337 ASSERT(list_empty(&vnode->writebacks));
337 ASSERT(!vnode->cb_promised); 338 ASSERT(!vnode->cb_promised);
338 339
339#ifdef AFS_CACHING_SUPPORT 340#ifdef AFS_CACHING_SUPPORT
@@ -350,3 +351,47 @@ void afs_clear_inode(struct inode *inode)
350 351
351 _leave(""); 352 _leave("");
352} 353}
354
355/*
356 * set the attributes of an inode
357 */
358int afs_setattr(struct dentry *dentry, struct iattr *attr)
359{
360 struct afs_vnode *vnode = AFS_FS_I(dentry->d_inode);
361 struct key *key;
362 int ret;
363
364 _enter("{%x:%u},{n=%s},%x",
365 vnode->fid.vid, vnode->fid.vnode, dentry->d_name.name,
366 attr->ia_valid);
367
368 if (!(attr->ia_valid & (ATTR_SIZE | ATTR_MODE | ATTR_UID | ATTR_GID |
369 ATTR_MTIME))) {
370 _leave(" = 0 [unsupported]");
371 return 0;
372 }
373
374 /* flush any dirty data outstanding on a regular file */
375 if (S_ISREG(vnode->vfs_inode.i_mode)) {
376 filemap_write_and_wait(vnode->vfs_inode.i_mapping);
377 afs_writeback_all(vnode);
378 }
379
380 if (attr->ia_valid & ATTR_FILE) {
381 key = attr->ia_file->private_data;
382 } else {
383 key = afs_request_key(vnode->volume->cell);
384 if (IS_ERR(key)) {
385 ret = PTR_ERR(key);
386 goto error;
387 }
388 }
389
390 ret = afs_vnode_setattr(vnode, key, attr);
391 if (!(attr->ia_valid & ATTR_FILE))
392 key_put(key);
393
394error:
395 _leave(" = %d", ret);
396 return ret;
397}
diff --git a/fs/afs/internal.h b/fs/afs/internal.h
index 9feb5c59d8fc..a30d4fa768e3 100644
--- a/fs/afs/internal.h
+++ b/fs/afs/internal.h
@@ -21,6 +21,7 @@
21 21
22#define AFS_CELL_MAX_ADDRS 15 22#define AFS_CELL_MAX_ADDRS 15
23 23
24struct pagevec;
24struct afs_call; 25struct afs_call;
25 26
26typedef enum { 27typedef enum {
@@ -75,12 +76,15 @@ struct afs_call {
75 struct key *key; /* security for this call */ 76 struct key *key; /* security for this call */
76 struct afs_server *server; /* server affected by incoming CM call */ 77 struct afs_server *server; /* server affected by incoming CM call */
77 void *request; /* request data (first part) */ 78 void *request; /* request data (first part) */
78 void *request2; /* request data (second part) */ 79 struct address_space *mapping; /* page set */
80 struct afs_writeback *wb; /* writeback being performed */
79 void *buffer; /* reply receive buffer */ 81 void *buffer; /* reply receive buffer */
80 void *reply; /* reply buffer (first part) */ 82 void *reply; /* reply buffer (first part) */
81 void *reply2; /* reply buffer (second part) */ 83 void *reply2; /* reply buffer (second part) */
82 void *reply3; /* reply buffer (third part) */ 84 void *reply3; /* reply buffer (third part) */
83 void *reply4; /* reply buffer (fourth part) */ 85 void *reply4; /* reply buffer (fourth part) */
86 pgoff_t first; /* first page in mapping to deal with */
87 pgoff_t last; /* last page in mapping to deal with */
84 enum { /* call state */ 88 enum { /* call state */
85 AFS_CALL_REQUESTING, /* request is being sent for outgoing call */ 89 AFS_CALL_REQUESTING, /* request is being sent for outgoing call */
86 AFS_CALL_AWAIT_REPLY, /* awaiting reply to outgoing call */ 90 AFS_CALL_AWAIT_REPLY, /* awaiting reply to outgoing call */
@@ -97,14 +101,18 @@ struct afs_call {
97 unsigned request_size; /* size of request data */ 101 unsigned request_size; /* size of request data */
98 unsigned reply_max; /* maximum size of reply */ 102 unsigned reply_max; /* maximum size of reply */
99 unsigned reply_size; /* current size of reply */ 103 unsigned reply_size; /* current size of reply */
104 unsigned first_offset; /* offset into mapping[first] */
105 unsigned last_to; /* amount of mapping[last] */
100 unsigned short offset; /* offset into received data store */ 106 unsigned short offset; /* offset into received data store */
101 unsigned char unmarshall; /* unmarshalling phase */ 107 unsigned char unmarshall; /* unmarshalling phase */
102 bool incoming; /* T if incoming call */ 108 bool incoming; /* T if incoming call */
109 bool send_pages; /* T if data from mapping should be sent */
103 u16 service_id; /* RxRPC service ID to call */ 110 u16 service_id; /* RxRPC service ID to call */
104 __be16 port; /* target UDP port */ 111 __be16 port; /* target UDP port */
105 __be32 operation_ID; /* operation ID for an incoming call */ 112 __be32 operation_ID; /* operation ID for an incoming call */
106 u32 count; /* count for use in unmarshalling */ 113 u32 count; /* count for use in unmarshalling */
107 __be32 tmp; /* place to extract temporary data */ 114 __be32 tmp; /* place to extract temporary data */
115 afs_dataversion_t store_version; /* updated version expected from store */
108}; 116};
109 117
110struct afs_call_type { 118struct afs_call_type {
@@ -124,6 +132,32 @@ struct afs_call_type {
124}; 132};
125 133
126/* 134/*
135 * record of an outstanding writeback on a vnode
136 */
137struct afs_writeback {
138 struct list_head link; /* link in vnode->writebacks */
139 struct work_struct writer; /* work item to perform the writeback */
140 struct afs_vnode *vnode; /* vnode to which this write applies */
141 struct key *key; /* owner of this write */
142 wait_queue_head_t waitq; /* completion and ready wait queue */
143 pgoff_t first; /* first page in batch */
144 pgoff_t point; /* last page in current store op */
145 pgoff_t last; /* last page in batch (inclusive) */
146 unsigned offset_first; /* offset into first page of start of write */
147 unsigned to_last; /* offset into last page of end of write */
148 int num_conflicts; /* count of conflicting writes in list */
149 int usage;
150 bool conflicts; /* T if has dependent conflicts */
151 enum {
152 AFS_WBACK_SYNCING, /* synchronisation being performed */
153 AFS_WBACK_PENDING, /* write pending */
154 AFS_WBACK_CONFLICTING, /* conflicting writes posted */
155 AFS_WBACK_WRITING, /* writing back */
156 AFS_WBACK_COMPLETE /* the writeback record has been unlinked */
157 } state __attribute__((packed));
158};
159
160/*
127 * AFS superblock private data 161 * AFS superblock private data
128 * - there's one superblock per volume 162 * - there's one superblock per volume
129 */ 163 */
@@ -305,6 +339,7 @@ struct afs_vnode {
305 wait_queue_head_t update_waitq; /* status fetch waitqueue */ 339 wait_queue_head_t update_waitq; /* status fetch waitqueue */
306 int update_cnt; /* number of outstanding ops that will update the 340 int update_cnt; /* number of outstanding ops that will update the
307 * status */ 341 * status */
342 spinlock_t writeback_lock; /* lock for writebacks */
308 spinlock_t lock; /* waitqueue/flags lock */ 343 spinlock_t lock; /* waitqueue/flags lock */
309 unsigned long flags; 344 unsigned long flags;
310#define AFS_VNODE_CB_BROKEN 0 /* set if vnode's callback was broken */ 345#define AFS_VNODE_CB_BROKEN 0 /* set if vnode's callback was broken */
@@ -316,6 +351,8 @@ struct afs_vnode {
316 351
317 long acl_order; /* ACL check count (callback break count) */ 352 long acl_order; /* ACL check count (callback break count) */
318 353
354 struct list_head writebacks; /* alterations in pagecache that need writing */
355
319 /* outstanding callback notification on this file */ 356 /* outstanding callback notification on this file */
320 struct rb_node server_rb; /* link in server->fs_vnodes */ 357 struct rb_node server_rb; /* link in server->fs_vnodes */
321 struct rb_node cb_promise; /* link in server->cb_promises */ 358 struct rb_node cb_promise; /* link in server->cb_promises */
@@ -463,6 +500,12 @@ extern int afs_fs_rename(struct afs_server *, struct key *,
463 struct afs_vnode *, const char *, 500 struct afs_vnode *, const char *,
464 struct afs_vnode *, const char *, 501 struct afs_vnode *, const char *,
465 const struct afs_wait_mode *); 502 const struct afs_wait_mode *);
503extern int afs_fs_store_data(struct afs_server *, struct afs_writeback *,
504 pgoff_t, pgoff_t, unsigned, unsigned,
505 const struct afs_wait_mode *);
506extern int afs_fs_setattr(struct afs_server *, struct key *,
507 struct afs_vnode *, struct iattr *,
508 const struct afs_wait_mode *);
466 509
467/* 510/*
468 * inode.c 511 * inode.c
@@ -473,6 +516,7 @@ extern struct inode *afs_iget(struct super_block *, struct key *,
473extern void afs_zap_data(struct afs_vnode *); 516extern void afs_zap_data(struct afs_vnode *);
474extern int afs_validate(struct afs_vnode *, struct key *); 517extern int afs_validate(struct afs_vnode *, struct key *);
475extern int afs_getattr(struct vfsmount *, struct dentry *, struct kstat *); 518extern int afs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
519extern int afs_setattr(struct dentry *, struct iattr *);
476extern void afs_clear_inode(struct inode *); 520extern void afs_clear_inode(struct inode *);
477 521
478/* 522/*
@@ -625,6 +669,9 @@ extern int afs_vnode_symlink(struct afs_vnode *, struct key *, const char *,
625 struct afs_file_status *, struct afs_server **); 669 struct afs_file_status *, struct afs_server **);
626extern int afs_vnode_rename(struct afs_vnode *, struct afs_vnode *, 670extern int afs_vnode_rename(struct afs_vnode *, struct afs_vnode *,
627 struct key *, const char *, const char *); 671 struct key *, const char *, const char *);
672extern int afs_vnode_store_data(struct afs_writeback *, pgoff_t, pgoff_t,
673 unsigned, unsigned);
674extern int afs_vnode_setattr(struct afs_vnode *, struct key *, struct iattr *);
628 675
629/* 676/*
630 * volume.c 677 * volume.c
@@ -641,6 +688,23 @@ extern struct afs_server *afs_volume_pick_fileserver(struct afs_vnode *);
641extern int afs_volume_release_fileserver(struct afs_vnode *, 688extern int afs_volume_release_fileserver(struct afs_vnode *,
642 struct afs_server *, int); 689 struct afs_server *, int);
643 690
691/*
692 * write.c
693 */
694extern int afs_set_page_dirty(struct page *);
695extern void afs_put_writeback(struct afs_writeback *);
696extern int afs_prepare_write(struct file *, struct page *, unsigned, unsigned);
697extern int afs_commit_write(struct file *, struct page *, unsigned, unsigned);
698extern int afs_writepage(struct page *, struct writeback_control *);
699extern int afs_writepages(struct address_space *, struct writeback_control *);
700extern int afs_write_inode(struct inode *, int);
701extern void afs_pages_written_back(struct afs_vnode *, struct afs_call *);
702extern ssize_t afs_file_write(struct kiocb *, const struct iovec *,
703 unsigned long, loff_t);
704extern int afs_writeback_all(struct afs_vnode *);
705extern int afs_fsync(struct file *, struct dentry *, int);
706
707
644/*****************************************************************************/ 708/*****************************************************************************/
645/* 709/*
646 * debug tracing 710 * debug tracing
diff --git a/fs/afs/rxrpc.c b/fs/afs/rxrpc.c
index 222c1a3abbb8..04189c47d6a0 100644
--- a/fs/afs/rxrpc.c
+++ b/fs/afs/rxrpc.c
@@ -237,6 +237,70 @@ void afs_flat_call_destructor(struct afs_call *call)
237} 237}
238 238
239/* 239/*
240 * attach the data from a bunch of pages on an inode to a call
241 */
242int afs_send_pages(struct afs_call *call, struct msghdr *msg, struct kvec *iov)
243{
244 struct page *pages[8];
245 unsigned count, n, loop, offset, to;
246 pgoff_t first = call->first, last = call->last;
247 int ret;
248
249 _enter("");
250
251 offset = call->first_offset;
252 call->first_offset = 0;
253
254 do {
255 _debug("attach %lx-%lx", first, last);
256
257 count = last - first + 1;
258 if (count > ARRAY_SIZE(pages))
259 count = ARRAY_SIZE(pages);
260 n = find_get_pages_contig(call->mapping, first, count, pages);
261 ASSERTCMP(n, ==, count);
262
263 loop = 0;
264 do {
265 msg->msg_flags = 0;
266 to = PAGE_SIZE;
267 if (first + loop >= last)
268 to = call->last_to;
269 else
270 msg->msg_flags = MSG_MORE;
271 iov->iov_base = kmap(pages[loop]) + offset;
272 iov->iov_len = to - offset;
273 offset = 0;
274
275 _debug("- range %u-%u%s",
276 offset, to, msg->msg_flags ? " [more]" : "");
277 msg->msg_iov = (struct iovec *) iov;
278 msg->msg_iovlen = 1;
279
280 /* have to change the state *before* sending the last
281 * packet as RxRPC might give us the reply before it
282 * returns from sending the request */
283 if (first + loop >= last)
284 call->state = AFS_CALL_AWAIT_REPLY;
285 ret = rxrpc_kernel_send_data(call->rxcall, msg,
286 to - offset);
287 kunmap(pages[loop]);
288 if (ret < 0)
289 break;
290 } while (++loop < count);
291 first += count;
292
293 for (loop = 0; loop < count; loop++)
294 put_page(pages[loop]);
295 if (ret < 0)
296 break;
297 } while (first < last);
298
299 _leave(" = %d", ret);
300 return ret;
301}
302
303/*
240 * initiate a call 304 * initiate a call
241 */ 305 */
242int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp, 306int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
@@ -253,8 +317,9 @@ int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
253 ASSERT(call->type != NULL); 317 ASSERT(call->type != NULL);
254 ASSERT(call->type->name != NULL); 318 ASSERT(call->type->name != NULL);
255 319
256 _debug("MAKE %p{%s} [%d]", 320 _debug("____MAKE %p{%s,%x} [%d]____",
257 call, call->type->name, atomic_read(&afs_outstanding_calls)); 321 call, call->type->name, key_serial(call->key),
322 atomic_read(&afs_outstanding_calls));
258 323
259 call->wait_mode = wait_mode; 324 call->wait_mode = wait_mode;
260 INIT_WORK(&call->async_work, afs_process_async_call); 325 INIT_WORK(&call->async_work, afs_process_async_call);
@@ -289,16 +354,23 @@ int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
289 msg.msg_iovlen = 1; 354 msg.msg_iovlen = 1;
290 msg.msg_control = NULL; 355 msg.msg_control = NULL;
291 msg.msg_controllen = 0; 356 msg.msg_controllen = 0;
292 msg.msg_flags = 0; 357 msg.msg_flags = (call->send_pages ? MSG_MORE : 0);
293 358
294 /* have to change the state *before* sending the last packet as RxRPC 359 /* have to change the state *before* sending the last packet as RxRPC
295 * might give us the reply before it returns from sending the 360 * might give us the reply before it returns from sending the
296 * request */ 361 * request */
297 call->state = AFS_CALL_AWAIT_REPLY; 362 if (!call->send_pages)
363 call->state = AFS_CALL_AWAIT_REPLY;
298 ret = rxrpc_kernel_send_data(rxcall, &msg, call->request_size); 364 ret = rxrpc_kernel_send_data(rxcall, &msg, call->request_size);
299 if (ret < 0) 365 if (ret < 0)
300 goto error_do_abort; 366 goto error_do_abort;
301 367
368 if (call->send_pages) {
369 ret = afs_send_pages(call, &msg, iov);
370 if (ret < 0)
371 goto error_do_abort;
372 }
373
302 /* at this point, an async call may no longer exist as it may have 374 /* at this point, an async call may no longer exist as it may have
303 * already completed */ 375 * already completed */
304 return wait_mode->wait(call); 376 return wait_mode->wait(call);
diff --git a/fs/afs/super.c b/fs/afs/super.c
index 7030d76155fc..d24be334b608 100644
--- a/fs/afs/super.c
+++ b/fs/afs/super.c
@@ -50,6 +50,7 @@ static const struct super_operations afs_super_ops = {
50 .statfs = simple_statfs, 50 .statfs = simple_statfs,
51 .alloc_inode = afs_alloc_inode, 51 .alloc_inode = afs_alloc_inode,
52 .drop_inode = generic_delete_inode, 52 .drop_inode = generic_delete_inode,
53 .write_inode = afs_write_inode,
53 .destroy_inode = afs_destroy_inode, 54 .destroy_inode = afs_destroy_inode,
54 .clear_inode = afs_clear_inode, 55 .clear_inode = afs_clear_inode,
55 .umount_begin = afs_umount_begin, 56 .umount_begin = afs_umount_begin,
@@ -66,7 +67,7 @@ enum {
66 afs_opt_vol, 67 afs_opt_vol,
67}; 68};
68 69
69static const match_table_t afs_options_list = { 70static match_table_t afs_options_list = {
70 { afs_opt_cell, "cell=%s" }, 71 { afs_opt_cell, "cell=%s" },
71 { afs_opt_rwpath, "rwpath" }, 72 { afs_opt_rwpath, "rwpath" },
72 { afs_opt_vol, "vol=%s" }, 73 { afs_opt_vol, "vol=%s" },
@@ -459,7 +460,9 @@ static void afs_i_init_once(void *_vnode, struct kmem_cache *cachep,
459 init_waitqueue_head(&vnode->update_waitq); 460 init_waitqueue_head(&vnode->update_waitq);
460 mutex_init(&vnode->permits_lock); 461 mutex_init(&vnode->permits_lock);
461 mutex_init(&vnode->validate_lock); 462 mutex_init(&vnode->validate_lock);
463 spin_lock_init(&vnode->writeback_lock);
462 spin_lock_init(&vnode->lock); 464 spin_lock_init(&vnode->lock);
465 INIT_LIST_HEAD(&vnode->writebacks);
463 INIT_WORK(&vnode->cb_broken_work, afs_broken_callback_work); 466 INIT_WORK(&vnode->cb_broken_work, afs_broken_callback_work);
464 } 467 }
465} 468}
diff --git a/fs/afs/vnode.c b/fs/afs/vnode.c
index 0e37f9949cb7..ec814660209f 100644
--- a/fs/afs/vnode.c
+++ b/fs/afs/vnode.c
@@ -753,3 +753,110 @@ no_server:
753 _leave(" = %ld [cnt %d]", PTR_ERR(server), orig_dvnode->update_cnt); 753 _leave(" = %ld [cnt %d]", PTR_ERR(server), orig_dvnode->update_cnt);
754 return PTR_ERR(server); 754 return PTR_ERR(server);
755} 755}
756
757/*
758 * write to a file
759 */
760int afs_vnode_store_data(struct afs_writeback *wb, pgoff_t first, pgoff_t last,
761 unsigned offset, unsigned to)
762{
763 struct afs_server *server;
764 struct afs_vnode *vnode = wb->vnode;
765 int ret;
766
767 _enter("%s{%x:%u.%u},%x,%lx,%lx,%x,%x",
768 vnode->volume->vlocation->vldb.name,
769 vnode->fid.vid,
770 vnode->fid.vnode,
771 vnode->fid.unique,
772 key_serial(wb->key),
773 first, last, offset, to);
774
775 /* this op will fetch the status */
776 spin_lock(&vnode->lock);
777 vnode->update_cnt++;
778 spin_unlock(&vnode->lock);
779
780 do {
781 /* pick a server to query */
782 server = afs_volume_pick_fileserver(vnode);
783 if (IS_ERR(server))
784 goto no_server;
785
786 _debug("USING SERVER: %08x\n", ntohl(server->addr.s_addr));
787
788 ret = afs_fs_store_data(server, wb, first, last, offset, to,
789 &afs_sync_call);
790
791 } while (!afs_volume_release_fileserver(vnode, server, ret));
792
793 /* adjust the flags */
794 if (ret == 0) {
795 afs_vnode_finalise_status_update(vnode, server);
796 afs_put_server(server);
797 } else {
798 afs_vnode_status_update_failed(vnode, ret);
799 }
800
801 _leave(" = %d", ret);
802 return ret;
803
804no_server:
805 spin_lock(&vnode->lock);
806 vnode->update_cnt--;
807 ASSERTCMP(vnode->update_cnt, >=, 0);
808 spin_unlock(&vnode->lock);
809 return PTR_ERR(server);
810}
811
812/*
813 * set the attributes on a file
814 */
815int afs_vnode_setattr(struct afs_vnode *vnode, struct key *key,
816 struct iattr *attr)
817{
818 struct afs_server *server;
819 int ret;
820
821 _enter("%s{%x:%u.%u},%x",
822 vnode->volume->vlocation->vldb.name,
823 vnode->fid.vid,
824 vnode->fid.vnode,
825 vnode->fid.unique,
826 key_serial(key));
827
828 /* this op will fetch the status */
829 spin_lock(&vnode->lock);
830 vnode->update_cnt++;
831 spin_unlock(&vnode->lock);
832
833 do {
834 /* pick a server to query */
835 server = afs_volume_pick_fileserver(vnode);
836 if (IS_ERR(server))
837 goto no_server;
838
839 _debug("USING SERVER: %08x\n", ntohl(server->addr.s_addr));
840
841 ret = afs_fs_setattr(server, key, vnode, attr, &afs_sync_call);
842
843 } while (!afs_volume_release_fileserver(vnode, server, ret));
844
845 /* adjust the flags */
846 if (ret == 0) {
847 afs_vnode_finalise_status_update(vnode, server);
848 afs_put_server(server);
849 } else {
850 afs_vnode_status_update_failed(vnode, ret);
851 }
852
853 _leave(" = %d", ret);
854 return ret;
855
856no_server:
857 spin_lock(&vnode->lock);
858 vnode->update_cnt--;
859 ASSERTCMP(vnode->update_cnt, >=, 0);
860 spin_unlock(&vnode->lock);
861 return PTR_ERR(server);
862}
diff --git a/fs/afs/write.c b/fs/afs/write.c
new file mode 100644
index 000000000000..83ff29262816
--- /dev/null
+++ b/fs/afs/write.c
@@ -0,0 +1,835 @@
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/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"
18
19static int afs_write_back_from_locked_page(struct afs_writeback *wb,
20 struct page *page);
21
22/*
23 * mark a page as having been made dirty and thus needing writeback
24 */
25int afs_set_page_dirty(struct page *page)
26{
27 _enter("");
28 return __set_page_dirty_nobuffers(page);
29}
30
31/*
32 * unlink a writeback record because its usage has reached zero
33 * - must be called with the wb->vnode->writeback_lock held
34 */
35static void afs_unlink_writeback(struct afs_writeback *wb)
36{
37 struct afs_writeback *front;
38 struct afs_vnode *vnode = wb->vnode;
39
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);
50 }
51 }
52}
53
54/*
55 * free a writeback record
56 */
57static void afs_free_writeback(struct afs_writeback *wb)
58{
59 _enter("");
60 key_put(wb->key);
61 kfree(wb);
62}
63
64/*
65 * dispose of a reference to a writeback record
66 */
67void afs_put_writeback(struct afs_writeback *wb)
68{
69 struct afs_vnode *vnode = wb->vnode;
70
71 _enter("{%d}", wb->usage);
72
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);
81}
82
83/*
84 * partly or wholly fill a page that's under preparation for writing
85 */
86static int afs_fill_page(struct afs_vnode *vnode, struct key *key,
87 unsigned start, unsigned len, struct page *page)
88{
89 int ret;
90
91 _enter(",,%u,%u", start, len);
92
93 ASSERTCMP(start + len, <=, PAGE_SIZE);
94
95 ret = afs_vnode_fetch_data(vnode, key, start, len, page);
96 if (ret < 0) {
97 if (ret == -ENOENT) {
98 _debug("got NOENT from server"
99 " - marking file deleted and stale");
100 set_bit(AFS_VNODE_DELETED, &vnode->flags);
101 ret = -ESTALE;
102 }
103 }
104
105 _leave(" = %d", ret);
106 return ret;
107}
108
109/*
110 * prepare a page for being written to
111 */
112static int afs_prepare_page(struct afs_vnode *vnode, struct page *page,
113 struct key *key, unsigned offset, unsigned to)
114{
115 unsigned eof, tail, start, stop, len;
116 loff_t i_size, pos;
117 void *p;
118 int ret;
119
120 _enter("");
121
122 if (offset == 0 && to == PAGE_SIZE)
123 return 0;
124
125 p = kmap(page);
126
127 i_size = i_size_read(&vnode->vfs_inode);
128 pos = (loff_t) page->index << PAGE_SHIFT;
129 if (pos >= i_size) {
130 /* partial write, page beyond EOF */
131 _debug("beyond");
132 if (offset > 0)
133 memset(p, 0, offset);
134 if (to < PAGE_SIZE)
135 memset(p + to, 0, PAGE_SIZE - to);
136 kunmap(page);
137 return 0;
138 }
139
140 if (i_size - pos >= PAGE_SIZE) {
141 /* partial write, page entirely before EOF */
142 _debug("before");
143 tail = eof = PAGE_SIZE;
144 } else {
145 /* partial write, page overlaps EOF */
146 eof = i_size - pos;
147 _debug("overlap %u", eof);
148 tail = max(eof, to);
149 if (tail < PAGE_SIZE)
150 memset(p + tail, 0, PAGE_SIZE - tail);
151 if (offset > eof)
152 memset(p + eof, 0, PAGE_SIZE - eof);
153 }
154
155 kunmap(p);
156
157 ret = 0;
158 if (offset > 0 || eof > to) {
159 /* need to fill one or two bits that aren't going to be written
160 * (cover both fillers in one read if there are two) */
161 start = (offset > 0) ? 0 : to;
162 stop = (eof > to) ? eof : offset;
163 len = stop - start;
164 _debug("wr=%u-%u av=0-%u rd=%u@%u",
165 offset, to, eof, start, len);
166 ret = afs_fill_page(vnode, key, start, len, page);
167 }
168
169 _leave(" = %d", ret);
170 return ret;
171}
172
173/*
174 * prepare to perform part of a write to a page
175 * - the caller holds the page locked, preventing it from being written out or
176 * modified by anyone else
177 */
178int afs_prepare_write(struct file *file, struct page *page,
179 unsigned offset, unsigned to)
180{
181 struct afs_writeback *candidate, *wb;
182 struct afs_vnode *vnode = AFS_FS_I(file->f_dentry->d_inode);
183 struct key *key = file->private_data;
184 pgoff_t index;
185 int ret;
186
187 _enter("{%x:%u},{%lx},%u,%u",
188 vnode->fid.vid, vnode->fid.vnode, page->index, offset, to);
189
190 candidate = kzalloc(sizeof(*candidate), GFP_KERNEL);
191 if (!candidate)
192 return -ENOMEM;
193 candidate->vnode = vnode;
194 candidate->first = candidate->last = page->index;
195 candidate->offset_first = offset;
196 candidate->to_last = to;
197 candidate->usage = 1;
198 candidate->state = AFS_WBACK_PENDING;
199 init_waitqueue_head(&candidate->waitq);
200
201 if (!PageUptodate(page)) {
202 _debug("not up to date");
203 ret = afs_prepare_page(vnode, page, key, offset, to);
204 if (ret < 0) {
205 kfree(candidate);
206 _leave(" = %d [prep]", ret);
207 return ret;
208 }
209 SetPageUptodate(page);
210 }
211
212try_again:
213 index = page->index;
214 spin_lock(&vnode->writeback_lock);
215
216 /* see if this page is already pending a writeback under a suitable key
217 * - if so we can just join onto that one */
218 wb = (struct afs_writeback *) page_private(page);
219 if (wb) {
220 if (wb->key == key && wb->state == AFS_WBACK_PENDING)
221 goto subsume_in_current_wb;
222 goto flush_conflicting_wb;
223 }
224
225 if (index > 0) {
226 /* see if we can find an already pending writeback that we can
227 * append this page to */
228 list_for_each_entry(wb, &vnode->writebacks, link) {
229 if (wb->last == index - 1 && wb->key == key &&
230 wb->state == AFS_WBACK_PENDING)
231 goto append_to_previous_wb;
232 }
233 }
234
235 list_add_tail(&candidate->link, &vnode->writebacks);
236 candidate->key = key_get(key);
237 spin_unlock(&vnode->writeback_lock);
238 SetPagePrivate(page);
239 set_page_private(page, (unsigned long) candidate);
240 _leave(" = 0 [new]");
241 return 0;
242
243subsume_in_current_wb:
244 _debug("subsume");
245 ASSERTRANGE(wb->first, <=, index, <=, wb->last);
246 if (index == wb->first && offset < wb->offset_first)
247 wb->offset_first = offset;
248 if (index == wb->last && to > wb->to_last)
249 wb->to_last = to;
250 spin_unlock(&vnode->writeback_lock);
251 kfree(candidate);
252 _leave(" = 0 [sub]");
253 return 0;
254
255append_to_previous_wb:
256 _debug("append into %lx-%lx", wb->first, wb->last);
257 wb->usage++;
258 wb->last++;
259 wb->to_last = to;
260 spin_unlock(&vnode->writeback_lock);
261 SetPagePrivate(page);
262 set_page_private(page, (unsigned long) wb);
263 kfree(candidate);
264 _leave(" = 0 [app]");
265 return 0;
266
267 /* the page is currently bound to another context, so if it's dirty we
268 * need to flush it before we can use the new context */
269flush_conflicting_wb:
270 _debug("flush conflict");
271 if (wb->state == AFS_WBACK_PENDING)
272 wb->state = AFS_WBACK_CONFLICTING;
273 spin_unlock(&vnode->writeback_lock);
274 if (PageDirty(page)) {
275 ret = afs_write_back_from_locked_page(wb, page);
276 if (ret < 0) {
277 afs_put_writeback(candidate);
278 _leave(" = %d", ret);
279 return ret;
280 }
281 }
282
283 /* the page holds a ref on the writeback record */
284 afs_put_writeback(wb);
285 set_page_private(page, 0);
286 ClearPagePrivate(page);
287 goto try_again;
288}
289
290/*
291 * finalise part of a write to a page
292 */
293int afs_commit_write(struct file *file, struct page *page,
294 unsigned offset, unsigned to)
295{
296 struct afs_vnode *vnode = AFS_FS_I(file->f_dentry->d_inode);
297 loff_t i_size, maybe_i_size;
298
299 _enter("{%x:%u},{%lx},%u,%u",
300 vnode->fid.vid, vnode->fid.vnode, page->index, offset, to);
301
302 maybe_i_size = (loff_t) page->index << PAGE_SHIFT;
303 maybe_i_size += to;
304
305 i_size = i_size_read(&vnode->vfs_inode);
306 if (maybe_i_size > i_size) {
307 spin_lock(&vnode->writeback_lock);
308 i_size = i_size_read(&vnode->vfs_inode);
309 if (maybe_i_size > i_size)
310 i_size_write(&vnode->vfs_inode, maybe_i_size);
311 spin_unlock(&vnode->writeback_lock);
312 }
313
314 set_page_dirty(page);
315
316 if (PageDirty(page))
317 _debug("dirtied");
318
319 return 0;
320}
321
322/*
323 * kill all the pages in the given range
324 */
325static void afs_kill_pages(struct afs_vnode *vnode, bool error,
326 pgoff_t first, pgoff_t last)
327{
328 struct pagevec pv;
329 unsigned count, loop;
330
331 _enter("{%x:%u},%lx-%lx",
332 vnode->fid.vid, vnode->fid.vnode, first, last);
333
334 pagevec_init(&pv, 0);
335
336 do {
337 _debug("kill %lx-%lx", first, last);
338
339 count = last - first + 1;
340 if (count > PAGEVEC_SIZE)
341 count = PAGEVEC_SIZE;
342 pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping,
343 first, count, pv.pages);
344 ASSERTCMP(pv.nr, ==, count);
345
346 for (loop = 0; loop < count; loop++) {
347 ClearPageUptodate(pv.pages[loop]);
348 if (error)
349 SetPageError(pv.pages[loop]);
350 end_page_writeback(pv.pages[loop]);
351 }
352
353 __pagevec_release(&pv);
354 } while (first < last);
355
356 _leave("");
357}
358
359/*
360 * synchronously write back the locked page and any subsequent non-locked dirty
361 * pages also covered by the same writeback record
362 */
363static int afs_write_back_from_locked_page(struct afs_writeback *wb,
364 struct page *primary_page)
365{
366 struct page *pages[8], *page;
367 unsigned long count;
368 unsigned n, offset, to;
369 pgoff_t start, first, last;
370 int loop, ret;
371
372 _enter(",%lx", primary_page->index);
373
374 count = 1;
375 if (!clear_page_dirty_for_io(primary_page))
376 BUG();
377 if (test_set_page_writeback(primary_page))
378 BUG();
379
380 /* find all consecutive lockable dirty pages, stopping when we find a
381 * page that is not immediately lockable, is not dirty or is missing,
382 * or we reach the end of the range */
383 start = primary_page->index;
384 if (start >= wb->last)
385 goto no_more;
386 start++;
387 do {
388 _debug("more %lx [%lx]", start, count);
389 n = wb->last - start + 1;
390 if (n > ARRAY_SIZE(pages))
391 n = ARRAY_SIZE(pages);
392 n = find_get_pages_contig(wb->vnode->vfs_inode.i_mapping,
393 start, n, pages);
394 _debug("fgpc %u", n);
395 if (n == 0)
396 goto no_more;
397 if (pages[0]->index != start) {
398 for (n--; n >= 0; n--)
399 put_page(pages[n]);
400 goto no_more;
401 }
402
403 for (loop = 0; loop < n; loop++) {
404 page = pages[loop];
405 if (page->index > wb->last)
406 break;
407 if (TestSetPageLocked(page))
408 break;
409 if (!PageDirty(page) ||
410 page_private(page) != (unsigned long) wb) {
411 unlock_page(page);
412 break;
413 }
414 if (!clear_page_dirty_for_io(page))
415 BUG();
416 if (test_set_page_writeback(page))
417 BUG();
418 unlock_page(page);
419 put_page(page);
420 }
421 count += loop;
422 if (loop < n) {
423 for (; loop < n; loop++)
424 put_page(pages[loop]);
425 goto no_more;
426 }
427
428 start += loop;
429 } while (start <= wb->last && count < 65536);
430
431no_more:
432 /* we now have a contiguous set of dirty pages, each with writeback set
433 * and the dirty mark cleared; the first page is locked and must remain
434 * so, all the rest are unlocked */
435 first = primary_page->index;
436 last = first + count - 1;
437
438 offset = (first == wb->first) ? wb->offset_first : 0;
439 to = (last == wb->last) ? wb->to_last : PAGE_SIZE;
440
441 _debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to);
442
443 ret = afs_vnode_store_data(wb, first, last, offset, to);
444 if (ret < 0) {
445 switch (ret) {
446 case -EDQUOT:
447 case -ENOSPC:
448 set_bit(AS_ENOSPC,
449 &wb->vnode->vfs_inode.i_mapping->flags);
450 break;
451 case -EROFS:
452 case -EIO:
453 case -EREMOTEIO:
454 case -EFBIG:
455 case -ENOENT:
456 case -ENOMEDIUM:
457 case -ENXIO:
458 afs_kill_pages(wb->vnode, true, first, last);
459 set_bit(AS_EIO, &wb->vnode->vfs_inode.i_mapping->flags);
460 break;
461 case -EACCES:
462 case -EPERM:
463 case -ENOKEY:
464 case -EKEYEXPIRED:
465 case -EKEYREJECTED:
466 case -EKEYREVOKED:
467 afs_kill_pages(wb->vnode, false, first, last);
468 break;
469 default:
470 break;
471 }
472 } else {
473 ret = count;
474 }
475
476 _leave(" = %d", ret);
477 return ret;
478}
479
480/*
481 * write a page back to the server
482 * - the caller locked the page for us
483 */
484int afs_writepage(struct page *page, struct writeback_control *wbc)
485{
486 struct backing_dev_info *bdi = page->mapping->backing_dev_info;
487 struct afs_writeback *wb;
488 int ret;
489
490 _enter("{%lx},", page->index);
491
492 if (wbc->sync_mode != WB_SYNC_NONE)
493 wait_on_page_writeback(page);
494
495 if (PageWriteback(page) || !PageDirty(page)) {
496 unlock_page(page);
497 return 0;
498 }
499
500 wb = (struct afs_writeback *) page_private(page);
501 ASSERT(wb != NULL);
502
503 ret = afs_write_back_from_locked_page(wb, page);
504 unlock_page(page);
505 if (ret < 0) {
506 _leave(" = %d", ret);
507 return 0;
508 }
509
510 wbc->nr_to_write -= ret;
511 if (wbc->nonblocking && bdi_write_congested(bdi))
512 wbc->encountered_congestion = 1;
513
514 _leave(" = 0");
515 return 0;
516}
517
518/*
519 * write a region of pages back to the server
520 */
521int afs_writepages_region(struct address_space *mapping,
522 struct writeback_control *wbc,
523 pgoff_t index, pgoff_t end, pgoff_t *_next)
524{
525 struct backing_dev_info *bdi = mapping->backing_dev_info;
526 struct afs_writeback *wb;
527 struct page *page;
528 int ret, n;
529
530 _enter(",,%lx,%lx,", index, end);
531
532 do {
533 n = find_get_pages_tag(mapping, &index, PAGECACHE_TAG_DIRTY,
534 1, &page);
535 if (!n)
536 break;
537
538 _debug("wback %lx", page->index);
539
540 if (page->index > end) {
541 *_next = index;
542 page_cache_release(page);
543 _leave(" = 0 [%lx]", *_next);
544 return 0;
545 }
546
547 /* at this point we hold neither mapping->tree_lock nor lock on
548 * the page itself: the page may be truncated or invalidated
549 * (changing page->mapping to NULL), or even swizzled back from
550 * swapper_space to tmpfs file mapping
551 */
552 lock_page(page);
553
554 if (page->mapping != mapping) {
555 unlock_page(page);
556 page_cache_release(page);
557 continue;
558 }
559
560 if (wbc->sync_mode != WB_SYNC_NONE)
561 wait_on_page_writeback(page);
562
563 if (PageWriteback(page) || !PageDirty(page)) {
564 unlock_page(page);
565 continue;
566 }
567
568 wb = (struct afs_writeback *) page_private(page);
569 ASSERT(wb != NULL);
570
571 spin_lock(&wb->vnode->writeback_lock);
572 wb->state = AFS_WBACK_WRITING;
573 spin_unlock(&wb->vnode->writeback_lock);
574
575 ret = afs_write_back_from_locked_page(wb, page);
576 unlock_page(page);
577 page_cache_release(page);
578 if (ret < 0) {
579 _leave(" = %d", ret);
580 return ret;
581 }
582
583 wbc->nr_to_write -= ret;
584
585 if (wbc->nonblocking && bdi_write_congested(bdi)) {
586 wbc->encountered_congestion = 1;
587 break;
588 }
589
590 cond_resched();
591 } while (index < end && wbc->nr_to_write > 0);
592
593 *_next = index;
594 _leave(" = 0 [%lx]", *_next);
595 return 0;
596}
597
598/*
599 * write some of the pending data back to the server
600 */
601int afs_writepages(struct address_space *mapping,
602 struct writeback_control *wbc)
603{
604 struct backing_dev_info *bdi = mapping->backing_dev_info;
605 pgoff_t start, end, next;
606 int ret;
607
608 _enter("");
609
610 if (wbc->nonblocking && bdi_write_congested(bdi)) {
611 wbc->encountered_congestion = 1;
612 _leave(" = 0 [congest]");
613 return 0;
614 }
615
616 if (wbc->range_cyclic) {
617 start = mapping->writeback_index;
618 end = -1;
619 ret = afs_writepages_region(mapping, wbc, start, end, &next);
620 if (start > 0 && wbc->nr_to_write > 0 && ret == 0 &&
621 !(wbc->nonblocking && wbc->encountered_congestion))
622 ret = afs_writepages_region(mapping, wbc, 0, start,
623 &next);
624 mapping->writeback_index = next;
625 } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
626 end = (pgoff_t)(LLONG_MAX >> PAGE_CACHE_SHIFT);
627 ret = afs_writepages_region(mapping, wbc, 0, end, &next);
628 if (wbc->nr_to_write > 0)
629 mapping->writeback_index = next;
630 } else {
631 start = wbc->range_start >> PAGE_CACHE_SHIFT;
632 end = wbc->range_end >> PAGE_CACHE_SHIFT;
633 ret = afs_writepages_region(mapping, wbc, start, end, &next);
634 }
635
636 _leave(" = %d", ret);
637 return ret;
638}
639
640/*
641 * write an inode back
642 */
643int afs_write_inode(struct inode *inode, int sync)
644{
645 struct afs_vnode *vnode = AFS_FS_I(inode);
646 int ret;
647
648 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
649
650 ret = 0;
651 if (sync) {
652 ret = filemap_fdatawait(inode->i_mapping);
653 if (ret < 0)
654 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
655 }
656
657 _leave(" = %d", ret);
658 return ret;
659}
660
661/*
662 * completion of write to server
663 */
664void afs_pages_written_back(struct afs_vnode *vnode, struct afs_call *call)
665{
666 struct afs_writeback *wb = call->wb;
667 struct pagevec pv;
668 unsigned count, loop;
669 pgoff_t first = call->first, last = call->last;
670 bool free_wb;
671
672 _enter("{%x:%u},{%lx-%lx}",
673 vnode->fid.vid, vnode->fid.vnode, first, last);
674
675 ASSERT(wb != NULL);
676
677 pagevec_init(&pv, 0);
678
679 do {
680 _debug("attach %lx-%lx", first, last);
681
682 count = last - first + 1;
683 if (count > PAGEVEC_SIZE)
684 count = PAGEVEC_SIZE;
685 pv.nr = find_get_pages_contig(call->mapping, first, count,
686 pv.pages);
687 ASSERTCMP(pv.nr, ==, count);
688
689 spin_lock(&vnode->writeback_lock);
690 for (loop = 0; loop < count; loop++) {
691 struct page *page = pv.pages[loop];
692 end_page_writeback(page);
693 if (page_private(page) == (unsigned long) wb) {
694 set_page_private(page, 0);
695 ClearPagePrivate(page);
696 wb->usage--;
697 }
698 }
699 free_wb = false;
700 if (wb->usage == 0) {
701 afs_unlink_writeback(wb);
702 free_wb = true;
703 }
704 spin_unlock(&vnode->writeback_lock);
705 first += count;
706 if (free_wb) {
707 afs_free_writeback(wb);
708 wb = NULL;
709 }
710
711 __pagevec_release(&pv);
712 } while (first < last);
713
714 _leave("");
715}
716
717/*
718 * write to an AFS file
719 */
720ssize_t afs_file_write(struct kiocb *iocb, const struct iovec *iov,
721 unsigned long nr_segs, loff_t pos)
722{
723 struct dentry *dentry = iocb->ki_filp->f_path.dentry;
724 struct afs_vnode *vnode = AFS_FS_I(dentry->d_inode);
725 ssize_t result;
726 size_t count = iov_length(iov, nr_segs);
727 int ret;
728
729 _enter("{%x.%u},{%zu},%lu,",
730 vnode->fid.vid, vnode->fid.vnode, count, nr_segs);
731
732 if (IS_SWAPFILE(&vnode->vfs_inode)) {
733 printk(KERN_INFO
734 "AFS: Attempt to write to active swap file!\n");
735 return -EBUSY;
736 }
737
738 if (!count)
739 return 0;
740
741 result = generic_file_aio_write(iocb, iov, nr_segs, pos);
742 if (IS_ERR_VALUE(result)) {
743 _leave(" = %zd", result);
744 return result;
745 }
746
747 /* return error values for O_SYNC and IS_SYNC() */
748 if (IS_SYNC(&vnode->vfs_inode) || iocb->ki_filp->f_flags & O_SYNC) {
749 ret = afs_fsync(iocb->ki_filp, dentry, 1);
750 if (ret < 0)
751 result = ret;
752 }
753
754 _leave(" = %zd", result);
755 return result;
756}
757
758/*
759 * flush the vnode to the fileserver
760 */
761int afs_writeback_all(struct afs_vnode *vnode)
762{
763 struct address_space *mapping = vnode->vfs_inode.i_mapping;
764 struct writeback_control wbc = {
765 .bdi = mapping->backing_dev_info,
766 .sync_mode = WB_SYNC_ALL,
767 .nr_to_write = LONG_MAX,
768 .for_writepages = 1,
769 .range_cyclic = 1,
770 };
771 int ret;
772
773 _enter("");
774
775 ret = mapping->a_ops->writepages(mapping, &wbc);
776 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
777
778 _leave(" = %d", ret);
779 return ret;
780}
781
782/*
783 * flush any dirty pages for this process, and check for write errors.
784 * - the return status from this call provides a reliable indication of
785 * whether any write errors occurred for this process.
786 */
787int afs_fsync(struct file *file, struct dentry *dentry, int datasync)
788{
789 struct afs_writeback *wb, *xwb;
790 struct afs_vnode *vnode = AFS_FS_I(dentry->d_inode);
791 int ret;
792
793 _enter("{%x:%u},{n=%s},%d",
794 vnode->fid.vid, vnode->fid.vnode, dentry->d_name.name,
795 datasync);
796
797 /* use a writeback record as a marker in the queue - when this reaches
798 * the front of the queue, all the outstanding writes are either
799 * completed or rejected */
800 wb = kzalloc(sizeof(*wb), GFP_KERNEL);
801 if (!wb)
802 return -ENOMEM;
803 wb->vnode = vnode;
804 wb->first = 0;
805 wb->last = -1;
806 wb->offset_first = 0;
807 wb->to_last = PAGE_SIZE;
808 wb->usage = 1;
809 wb->state = AFS_WBACK_SYNCING;
810 init_waitqueue_head(&wb->waitq);
811
812 spin_lock(&vnode->writeback_lock);
813 list_for_each_entry(xwb, &vnode->writebacks, link) {
814 if (xwb->state == AFS_WBACK_PENDING)
815 xwb->state = AFS_WBACK_CONFLICTING;
816 }
817 list_add_tail(&wb->link, &vnode->writebacks);
818 spin_unlock(&vnode->writeback_lock);
819
820 /* push all the outstanding writebacks to the server */
821 ret = afs_writeback_all(vnode);
822 if (ret < 0) {
823 afs_put_writeback(wb);
824 _leave(" = %d [wb]", ret);
825 return ret;
826 }
827
828 /* wait for the preceding writes to actually complete */
829 ret = wait_event_interruptible(wb->waitq,
830 wb->state == AFS_WBACK_COMPLETE ||
831 vnode->writebacks.next == &wb->link);
832 afs_put_writeback(wb);
833 _leave(" = %d", ret);
834 return ret;
835}