Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
| 3 | Copyright (C) 2001 Miklos Szeredi (mszeredi@inf.bme.hu) |
| 4 | |
| 5 | This program can be distributed under the terms of the GNU GPL. |
| 6 | See the file COPYING. |
| 7 | */ |
| 8 | #include "fuse_i.h" |
| 9 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 10 | #include <linux/pagemap.h> |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 11 | #include <linux/slab.h> |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 12 | #ifdef KERNEL_2_6 |
| 13 | #include <linux/backing-dev.h> |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 14 | #include <linux/writeback.h> |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 15 | #endif |
| 16 | |
| 17 | #ifndef KERNEL_2_6 |
Miklos Szeredi | 920bab2 | 2004-01-26 14:18:43 +0000 | [diff] [blame] | 18 | #define PageUptodate(page) Page_Uptodate(page) |
| 19 | #define filemap_fdatawrite filemap_fdatasync |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 20 | #endif |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 21 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 22 | static int fuse_open(struct inode *inode, struct file *file) |
| 23 | { |
| 24 | struct fuse_conn *fc = INO_FC(inode); |
| 25 | struct fuse_in in = FUSE_IN_INIT; |
| 26 | struct fuse_out out = FUSE_OUT_INIT; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 27 | struct fuse_open_in inarg; |
Miklos Szeredi | f58cc61 | 2004-02-06 13:52:00 +0000 | [diff] [blame] | 28 | int err; |
| 29 | |
| 30 | err = generic_file_open(inode, file); |
| 31 | if(err) |
| 32 | return err; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 33 | |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 34 | /* If opening the root node, no lookup has been performed on |
| 35 | it, so the attributes must be refreshed */ |
| 36 | if(inode->i_ino == FUSE_ROOT_INO) { |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 37 | int err = fuse_do_getattr(inode); |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 38 | if(err) |
| 39 | return err; |
| 40 | } |
| 41 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 42 | memset(&inarg, 0, sizeof(inarg)); |
| 43 | inarg.flags = file->f_flags & ~O_EXCL; |
| 44 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 45 | in.h.opcode = FUSE_OPEN; |
| 46 | in.h.ino = inode->i_ino; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 47 | in.numargs = 1; |
| 48 | in.args[0].size = sizeof(inarg); |
| 49 | in.args[0].value = &inarg; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 50 | request_send(fc, &in, &out); |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 51 | if(!out.h.error && !(fc->flags & FUSE_KERNEL_CACHE)) { |
| 52 | #ifdef KERNEL_2_6 |
| 53 | invalidate_inode_pages(inode->i_mapping); |
| 54 | #else |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 55 | invalidate_inode_pages(inode); |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 56 | #endif |
| 57 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 58 | |
| 59 | return out.h.error; |
| 60 | } |
| 61 | |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 62 | static int fuse_release(struct inode *inode, struct file *file) |
| 63 | { |
| 64 | struct fuse_conn *fc = INO_FC(inode); |
Miklos Szeredi | 383a9df | 2002-12-10 14:54:57 +0000 | [diff] [blame] | 65 | struct fuse_in *in = NULL; |
Miklos Szeredi | 9478e86 | 2002-12-11 09:50:26 +0000 | [diff] [blame] | 66 | struct fuse_open_in *inarg = NULL; |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 67 | unsigned int s = sizeof(struct fuse_in) + sizeof(struct fuse_open_in); |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 68 | |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 69 | if(file->f_mode & FMODE_WRITE) |
| 70 | filemap_fdatawrite(inode->i_mapping); |
| 71 | |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 72 | in = kmalloc(s, GFP_NOFS); |
Miklos Szeredi | 383a9df | 2002-12-10 14:54:57 +0000 | [diff] [blame] | 73 | if(!in) |
| 74 | return -ENOMEM; |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 75 | memset(in, 0, s); |
| 76 | inarg = (struct fuse_open_in *) (in + 1); |
Miklos Szeredi | 9478e86 | 2002-12-11 09:50:26 +0000 | [diff] [blame] | 77 | inarg->flags = file->f_flags & ~O_EXCL; |
Miklos Szeredi | 383a9df | 2002-12-10 14:54:57 +0000 | [diff] [blame] | 78 | |
| 79 | in->h.opcode = FUSE_RELEASE; |
| 80 | in->h.ino = inode->i_ino; |
Miklos Szeredi | 9478e86 | 2002-12-11 09:50:26 +0000 | [diff] [blame] | 81 | in->numargs = 1; |
| 82 | in->args[0].size = sizeof(struct fuse_open_in); |
| 83 | in->args[0].value = inarg; |
Miklos Szeredi | 383a9df | 2002-12-10 14:54:57 +0000 | [diff] [blame] | 84 | if(!request_send_noreply(fc, in)) |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 85 | return 0; |
| 86 | |
Miklos Szeredi | 383a9df | 2002-12-10 14:54:57 +0000 | [diff] [blame] | 87 | kfree(in); |
| 88 | return 0; |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Miklos Szeredi | e4cf733 | 2003-12-12 11:53:31 +0000 | [diff] [blame] | 91 | static int fuse_fsync(struct file *file, struct dentry *de, int datasync) |
| 92 | { |
Miklos Szeredi | 5e43f2c | 2003-12-12 14:06:41 +0000 | [diff] [blame] | 93 | struct inode *inode = de->d_inode; |
| 94 | struct fuse_conn *fc = INO_FC(inode); |
| 95 | struct fuse_in in = FUSE_IN_INIT; |
| 96 | struct fuse_out out = FUSE_OUT_INIT; |
| 97 | struct fuse_fsync_in inarg; |
| 98 | |
| 99 | memset(&inarg, 0, sizeof(inarg)); |
| 100 | inarg.datasync = datasync; |
| 101 | |
| 102 | in.h.opcode = FUSE_FSYNC; |
| 103 | in.h.ino = inode->i_ino; |
| 104 | in.numargs = 1; |
| 105 | in.args[0].size = sizeof(inarg); |
| 106 | in.args[0].value = &inarg; |
| 107 | request_send(fc, &in, &out); |
| 108 | return out.h.error; |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 109 | |
| 110 | /* FIXME: need to ensure, that all write requests issued |
| 111 | before this request are completed. Should userspace take |
| 112 | care of this? */ |
Miklos Szeredi | e4cf733 | 2003-12-12 11:53:31 +0000 | [diff] [blame] | 113 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 114 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 115 | static int fuse_readpage(struct file *file, struct page *page) |
| 116 | { |
| 117 | struct inode *inode = page->mapping->host; |
| 118 | struct fuse_conn *fc = INO_FC(inode); |
| 119 | struct fuse_in in = FUSE_IN_INIT; |
| 120 | struct fuse_out out = FUSE_OUT_INIT; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 121 | struct fuse_read_in inarg; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 122 | char *buffer; |
| 123 | |
| 124 | buffer = kmap(page); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 125 | |
| 126 | memset(&inarg, 0, sizeof(inarg)); |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 127 | inarg.offset = (unsigned long long) page->index << PAGE_CACHE_SHIFT; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 128 | inarg.size = PAGE_CACHE_SIZE; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 129 | |
| 130 | in.h.opcode = FUSE_READ; |
| 131 | in.h.ino = inode->i_ino; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 132 | in.numargs = 1; |
| 133 | in.args[0].size = sizeof(inarg); |
| 134 | in.args[0].value = &inarg; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 135 | out.argvar = 1; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 136 | out.numargs = 1; |
| 137 | out.args[0].size = PAGE_CACHE_SIZE; |
| 138 | out.args[0].value = buffer; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 139 | |
| 140 | request_send(fc, &in, &out); |
| 141 | if(!out.h.error) { |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 142 | size_t outsize = out.args[0].size; |
| 143 | if(outsize < PAGE_CACHE_SIZE) |
| 144 | memset(buffer + outsize, 0, PAGE_CACHE_SIZE - outsize); |
Miklos Szeredi | d1199f8 | 2004-02-06 15:29:22 +0000 | [diff] [blame] | 145 | flush_dcache_page(page); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 146 | SetPageUptodate(page); |
| 147 | } |
| 148 | |
| 149 | kunmap(page); |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 150 | unlock_page(page); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 151 | |
| 152 | return out.h.error; |
| 153 | } |
| 154 | |
Miklos Szeredi | 36ca556 | 2003-11-03 19:32:14 +0000 | [diff] [blame] | 155 | static int fuse_is_block_uptodate(struct address_space *mapping, |
| 156 | struct inode *inode, size_t bl_index) |
| 157 | { |
| 158 | size_t index = bl_index << FUSE_BLOCK_PAGE_SHIFT; |
| 159 | size_t end_index = ((bl_index + 1) << FUSE_BLOCK_PAGE_SHIFT) - 1; |
Miklos Szeredi | d1199f8 | 2004-02-06 15:29:22 +0000 | [diff] [blame] | 160 | size_t file_end_index = i_size_read(inode) >> PAGE_CACHE_SHIFT; |
Miklos Szeredi | 36ca556 | 2003-11-03 19:32:14 +0000 | [diff] [blame] | 161 | |
| 162 | if (end_index > file_end_index) |
| 163 | end_index = file_end_index; |
| 164 | |
| 165 | for (; index <= end_index; index++) { |
| 166 | struct page *page = find_get_page(mapping, index); |
| 167 | |
| 168 | if (!page) |
| 169 | return 0; |
| 170 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 171 | if (!PageUptodate(page)) { |
Miklos Szeredi | 36ca556 | 2003-11-03 19:32:14 +0000 | [diff] [blame] | 172 | page_cache_release(page); |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | page_cache_release(page); |
| 177 | } |
| 178 | |
| 179 | return 1; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | static int fuse_cache_block(struct address_space *mapping, |
| 184 | struct inode *inode, char *bl_buf, |
| 185 | size_t bl_index) |
| 186 | { |
| 187 | size_t start_index = bl_index << FUSE_BLOCK_PAGE_SHIFT; |
| 188 | size_t end_index = ((bl_index + 1) << FUSE_BLOCK_PAGE_SHIFT) - 1; |
Miklos Szeredi | d1199f8 | 2004-02-06 15:29:22 +0000 | [diff] [blame] | 189 | size_t file_end_index = i_size_read(inode) >> PAGE_CACHE_SHIFT; |
Miklos Szeredi | 36ca556 | 2003-11-03 19:32:14 +0000 | [diff] [blame] | 190 | |
Michael Grigoriev | 37b3f3b | 2003-11-09 15:33:24 +0000 | [diff] [blame] | 191 | int i; |
Miklos Szeredi | 36ca556 | 2003-11-03 19:32:14 +0000 | [diff] [blame] | 192 | |
| 193 | if (end_index > file_end_index) |
| 194 | end_index = file_end_index; |
| 195 | |
| 196 | for (i = 0; start_index + i <= end_index; i++) { |
| 197 | size_t index = start_index + i; |
| 198 | struct page *page; |
| 199 | char *buffer; |
| 200 | |
Miklos Szeredi | e4cf733 | 2003-12-12 11:53:31 +0000 | [diff] [blame] | 201 | page = grab_cache_page(mapping, index); |
Michael Grigoriev | 37b3f3b | 2003-11-09 15:33:24 +0000 | [diff] [blame] | 202 | if (!page) |
| 203 | return -1; |
| 204 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 205 | if (!PageUptodate(page)) { |
Miklos Szeredi | 36ca556 | 2003-11-03 19:32:14 +0000 | [diff] [blame] | 206 | buffer = kmap(page); |
| 207 | memcpy(buffer, bl_buf + i * PAGE_CACHE_SIZE, |
| 208 | PAGE_CACHE_SIZE); |
Miklos Szeredi | 55a078f | 2004-02-06 16:08:03 +0000 | [diff] [blame] | 209 | flush_dcache_page(page); |
Miklos Szeredi | 36ca556 | 2003-11-03 19:32:14 +0000 | [diff] [blame] | 210 | SetPageUptodate(page); |
| 211 | kunmap(page); |
| 212 | } |
| 213 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 214 | unlock_page(page); |
Miklos Szeredi | 36ca556 | 2003-11-03 19:32:14 +0000 | [diff] [blame] | 215 | page_cache_release(page); |
| 216 | } |
| 217 | |
Michael Grigoriev | 37b3f3b | 2003-11-09 15:33:24 +0000 | [diff] [blame] | 218 | return 0; |
Miklos Szeredi | 36ca556 | 2003-11-03 19:32:14 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | static int fuse_file_read_block(struct inode *inode, char *bl_buf, |
| 222 | size_t bl_index) |
| 223 | { |
| 224 | struct fuse_conn *fc = INO_FC(inode); |
| 225 | struct fuse_in in = FUSE_IN_INIT; |
| 226 | struct fuse_out out = FUSE_OUT_INIT; |
| 227 | struct fuse_read_in inarg; |
| 228 | |
| 229 | memset(&inarg, 0, sizeof(inarg)); |
| 230 | inarg.offset = bl_index << FUSE_BLOCK_SHIFT; |
| 231 | inarg.size = FUSE_BLOCK_SIZE; |
| 232 | |
| 233 | in.h.opcode = FUSE_READ; |
| 234 | in.h.ino = inode->i_ino; |
| 235 | in.numargs = 1; |
| 236 | in.args[0].size = sizeof(inarg); |
| 237 | in.args[0].value = &inarg; |
| 238 | out.argvar = 1; |
| 239 | out.numargs = 1; |
| 240 | out.args[0].size = FUSE_BLOCK_SIZE; |
| 241 | out.args[0].value = bl_buf; |
| 242 | |
| 243 | request_send(fc, &in, &out); |
| 244 | |
| 245 | if (!out.h.error) { |
| 246 | size_t outsize = out.args[0].size; |
| 247 | if (outsize < FUSE_BLOCK_SIZE) |
| 248 | memset(bl_buf + outsize, 0, FUSE_BLOCK_SIZE - outsize); |
| 249 | } |
| 250 | |
| 251 | return out.h.error; |
| 252 | } |
| 253 | |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 254 | static void fuse_file_bigread(struct address_space *mapping, |
| 255 | struct inode *inode, loff_t pos, size_t count) |
| 256 | { |
| 257 | size_t bl_index = pos >> FUSE_BLOCK_SHIFT; |
| 258 | size_t bl_end_index = (pos + count) >> FUSE_BLOCK_SHIFT; |
Miklos Szeredi | d1199f8 | 2004-02-06 15:29:22 +0000 | [diff] [blame] | 259 | size_t bl_file_end_index = i_size_read(inode) >> FUSE_BLOCK_SHIFT; |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 260 | |
| 261 | if (bl_end_index > bl_file_end_index) |
| 262 | bl_end_index = bl_file_end_index; |
| 263 | |
| 264 | while (bl_index <= bl_end_index) { |
| 265 | int res; |
| 266 | char *bl_buf = kmalloc(FUSE_BLOCK_SIZE, GFP_NOFS); |
| 267 | if (!bl_buf) |
| 268 | break; |
| 269 | res = fuse_is_block_uptodate(mapping, inode, bl_index); |
| 270 | if (!res) |
| 271 | res = fuse_file_read_block(inode, bl_buf, bl_index); |
| 272 | if (!res) |
| 273 | fuse_cache_block(mapping, inode, bl_buf, bl_index); |
| 274 | kfree(bl_buf); |
| 275 | bl_index++; |
| 276 | } |
| 277 | } |
| 278 | |
Miklos Szeredi | 36ca556 | 2003-11-03 19:32:14 +0000 | [diff] [blame] | 279 | static ssize_t fuse_file_read(struct file *filp, char *buf, |
| 280 | size_t count, loff_t * ppos) |
| 281 | { |
| 282 | struct address_space *mapping = filp->f_dentry->d_inode->i_mapping; |
| 283 | struct inode *inode = mapping->host; |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 284 | struct fuse_conn *fc = INO_FC(inode); |
Miklos Szeredi | 36ca556 | 2003-11-03 19:32:14 +0000 | [diff] [blame] | 285 | |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 286 | if(fc->flags & FUSE_LARGE_READ) |
| 287 | fuse_file_bigread(mapping, inode, *ppos, count); |
Miklos Szeredi | 36ca556 | 2003-11-03 19:32:14 +0000 | [diff] [blame] | 288 | |
| 289 | return generic_file_read(filp, buf, count, ppos); |
| 290 | } |
| 291 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 292 | static int write_buffer(struct inode *inode, struct page *page, |
| 293 | unsigned offset, size_t count) |
| 294 | { |
| 295 | struct fuse_conn *fc = INO_FC(inode); |
| 296 | struct fuse_in in = FUSE_IN_INIT; |
| 297 | struct fuse_out out = FUSE_OUT_INIT; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 298 | struct fuse_write_in inarg; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 299 | char *buffer; |
| 300 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 301 | buffer = kmap(page); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 302 | |
| 303 | memset(&inarg, 0, sizeof(inarg)); |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 304 | inarg.offset = ((unsigned long long) page->index << PAGE_CACHE_SHIFT) + |
| 305 | offset; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 306 | inarg.size = count; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 307 | |
| 308 | in.h.opcode = FUSE_WRITE; |
| 309 | in.h.ino = inode->i_ino; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 310 | in.numargs = 2; |
| 311 | in.args[0].size = sizeof(inarg); |
| 312 | in.args[0].value = &inarg; |
| 313 | in.args[1].size = count; |
| 314 | in.args[1].value = buffer + offset; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 315 | request_send(fc, &in, &out); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 316 | kunmap(page); |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 317 | if(out.h.error) |
| 318 | SetPageError(page); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 319 | |
| 320 | return out.h.error; |
| 321 | } |
| 322 | |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 323 | static int get_write_count(struct inode *inode, struct page *page) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 324 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 325 | unsigned long end_index; |
Miklos Szeredi | d1199f8 | 2004-02-06 15:29:22 +0000 | [diff] [blame] | 326 | loff_t size = i_size_read(inode); |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 327 | int count; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 328 | |
Miklos Szeredi | d1199f8 | 2004-02-06 15:29:22 +0000 | [diff] [blame] | 329 | end_index = size >> PAGE_CACHE_SHIFT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 330 | if(page->index < end_index) |
| 331 | count = PAGE_CACHE_SIZE; |
| 332 | else { |
Miklos Szeredi | d1199f8 | 2004-02-06 15:29:22 +0000 | [diff] [blame] | 333 | count = size & (PAGE_CACHE_SIZE - 1); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 334 | if(page->index > end_index || count == 0) |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 335 | return 0; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 336 | } |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 337 | return count; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 340 | #ifdef KERNEL_2_6 |
| 341 | |
| 342 | static void write_buffer_end(struct fuse_conn *fc, struct fuse_in *in, |
| 343 | struct fuse_out *out, void *_page) |
| 344 | { |
| 345 | struct page *page = (struct page *) _page; |
| 346 | |
| 347 | lock_page(page); |
| 348 | if(out->h.error) { |
| 349 | SetPageError(page); |
| 350 | if(out->h.error == -ENOSPC) |
| 351 | set_bit(AS_ENOSPC, &page->mapping->flags); |
| 352 | else |
| 353 | set_bit(AS_EIO, &page->mapping->flags); |
| 354 | } |
| 355 | end_page_writeback(page); |
| 356 | kunmap(page); |
| 357 | unlock_page(page); |
| 358 | kfree(in); |
| 359 | } |
| 360 | |
| 361 | static int write_buffer_nonblock(struct inode *inode, struct page *page, |
| 362 | unsigned offset, size_t count) |
| 363 | { |
| 364 | int err; |
| 365 | struct fuse_conn *fc = INO_FC(inode); |
| 366 | struct fuse_in *in = NULL; |
| 367 | struct fuse_out *out = NULL; |
| 368 | struct fuse_write_in *inarg = NULL; |
| 369 | char *buffer; |
| 370 | unsigned int s = sizeof(struct fuse_in) + sizeof(struct fuse_out) + |
| 371 | sizeof(struct fuse_write_in); |
| 372 | |
| 373 | in = kmalloc(s, GFP_NOFS); |
| 374 | if(!in) |
| 375 | return -ENOMEM; |
| 376 | memset(in, 0, s); |
| 377 | out = (struct fuse_out *)(in + 1); |
| 378 | inarg = (struct fuse_write_in *)(out + 1); |
| 379 | |
| 380 | buffer = kmap(page); |
| 381 | |
| 382 | inarg->offset = ((unsigned long long) page->index << PAGE_CACHE_SHIFT) + offset; |
| 383 | inarg->size = count; |
| 384 | |
| 385 | in->h.opcode = FUSE_WRITE; |
| 386 | in->h.ino = inode->i_ino; |
| 387 | in->numargs = 2; |
| 388 | in->args[0].size = sizeof(struct fuse_write_in); |
| 389 | in->args[0].value = inarg; |
| 390 | in->args[1].size = count; |
| 391 | in->args[1].value = buffer + offset; |
| 392 | err = request_send_nonblock(fc, in, out, write_buffer_end, page); |
| 393 | if(err) { |
| 394 | if(err != -EWOULDBLOCK) |
| 395 | SetPageError(page); |
| 396 | kunmap(page); |
| 397 | kfree(in); |
| 398 | } |
| 399 | return err; |
| 400 | } |
| 401 | |
| 402 | static int fuse_writepage(struct page *page, struct writeback_control *wbc) |
| 403 | { |
| 404 | int err; |
| 405 | struct inode *inode = page->mapping->host; |
| 406 | unsigned count = get_write_count(inode, page); |
| 407 | |
| 408 | err = -EINVAL; |
| 409 | if(count) { |
| 410 | /* FIXME: check sync_mode, and wait for previous writes (or |
| 411 | signal userspace to do this) */ |
| 412 | if(wbc->nonblocking) { |
| 413 | err = write_buffer_nonblock(inode, page, 0, count); |
| 414 | if(!err) |
| 415 | SetPageWriteback(page); |
| 416 | else if(err == -EWOULDBLOCK) { |
| 417 | __set_page_dirty_nobuffers(page); |
| 418 | err = 0; |
| 419 | } |
| 420 | } else |
| 421 | err = write_buffer(inode, page, 0, count); |
| 422 | } |
| 423 | |
| 424 | unlock_page(page); |
| 425 | return err; |
| 426 | } |
| 427 | #else |
| 428 | static int fuse_writepage(struct page *page) |
| 429 | { |
| 430 | int err; |
| 431 | struct inode *inode = page->mapping->host; |
| 432 | int count = get_write_count(inode, page); |
| 433 | err = -EINVAL; |
| 434 | if(count) |
| 435 | err = write_buffer(inode, page, 0, count); |
| 436 | |
| 437 | unlock_page(page); |
| 438 | return err; |
| 439 | } |
| 440 | #endif |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 441 | |
| 442 | static int fuse_prepare_write(struct file *file, struct page *page, |
| 443 | unsigned offset, unsigned to) |
| 444 | { |
| 445 | /* No op */ |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | static int fuse_commit_write(struct file *file, struct page *page, |
| 450 | unsigned offset, unsigned to) |
| 451 | { |
| 452 | int err; |
| 453 | struct inode *inode = page->mapping->host; |
| 454 | |
| 455 | err = write_buffer(inode, page, offset, to - offset); |
| 456 | if(!err) { |
| 457 | loff_t pos = (page->index << PAGE_CACHE_SHIFT) + to; |
Miklos Szeredi | d1199f8 | 2004-02-06 15:29:22 +0000 | [diff] [blame] | 458 | if(pos > i_size_read(inode)) |
| 459 | i_size_write(inode, pos); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 460 | } |
| 461 | return err; |
| 462 | } |
| 463 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 464 | static struct file_operations fuse_file_operations = { |
Miklos Szeredi | 89b86af | 2004-02-06 17:02:08 +0000 | [diff] [blame] | 465 | .read = fuse_file_read, |
| 466 | .write = generic_file_write, |
| 467 | .mmap = generic_file_mmap, |
| 468 | .open = fuse_open, |
| 469 | .release = fuse_release, |
| 470 | .fsync = fuse_fsync, |
| 471 | #ifdef KERNEL_2_6 |
| 472 | .sendfile = generic_file_sendfile, |
| 473 | #endif |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 474 | }; |
| 475 | |
| 476 | static struct address_space_operations fuse_file_aops = { |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 477 | .readpage = fuse_readpage, |
| 478 | .writepage = fuse_writepage, |
| 479 | .prepare_write = fuse_prepare_write, |
| 480 | .commit_write = fuse_commit_write, |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 481 | }; |
| 482 | |
| 483 | void fuse_init_file_inode(struct inode *inode) |
| 484 | { |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 485 | #ifdef KERNEL_2_6 |
| 486 | struct fuse_conn *fc = INO_FC(inode); |
| 487 | /* Readahead somehow defeats big reads on 2.6 (says Michael |
| 488 | Grigoriev) */ |
| 489 | if(fc->flags & FUSE_LARGE_READ) |
| 490 | inode->i_mapping->backing_dev_info->ra_pages = 0; |
| 491 | #endif |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 492 | inode->i_fop = &fuse_file_operations; |
| 493 | inode->i_data.a_ops = &fuse_file_aops; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | /* |
| 497 | * Local Variables: |
| 498 | * indent-tabs-mode: t |
| 499 | * c-basic-offset: 8 |
| 500 | * End: |
| 501 | */ |