12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/module.h>
32 memset(&inarg, 0,
sizeof(inarg));
37 req->
in.h.nodeid = nodeid;
39 req->
in.args[0].size =
sizeof(inarg);
40 req->
in.args[0].value = &inarg;
42 req->
out.args[0].size =
sizeof(*outargp);
43 req->
out.args[0].value = outargp;
45 err = req->
out.h.error;
73 spin_unlock(&fc->
lock);
97 path = req->misc.release.path;
133 req->
end = fuse_release_end;
152 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
173 struct fuse_conn *fc = get_fuse_conn(inode);
176 file->
f_op = &fuse_direct_io_file_operations;
182 struct fuse_inode *fi = get_fuse_inode(inode);
184 spin_lock(&fc->
lock);
186 i_size_write(inode, 0);
187 spin_unlock(&fc->
lock);
194 struct fuse_conn *fc = get_fuse_conn(inode);
201 err =
fuse_do_open(fc, get_node_id(inode), file, isdir);
210 static void fuse_prepare_release(
struct fuse_file *ff,
int flags,
int opcode)
216 spin_lock(&fc->
lock);
220 spin_unlock(&fc->
lock);
230 req->
in.args[0].value = inarg;
243 fuse_prepare_release(ff, file->
f_flags, opcode);
264 fuse_file_put(ff, ff->
fc->destroy_req !=
NULL);
267 static int fuse_open(
struct inode *
inode,
struct file *file)
272 static int fuse_release(
struct inode *
inode,
struct file *file)
304 for (i = 0; i < 32; i++) {
305 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
307 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
310 return (
u64) v0 + ((
u64) v1 << 32);
321 struct fuse_conn *fc = get_fuse_conn(inode);
322 struct fuse_inode *fi = get_fuse_inode(inode);
326 spin_lock(&fc->
lock);
332 if (curr_index == index) {
337 spin_unlock(&fc->
lock);
348 static int fuse_wait_on_page_writeback(
struct inode *inode,
pgoff_t index)
350 struct fuse_inode *fi = get_fuse_inode(inode);
356 static int fuse_flush(
struct file *file,
fl_owner_t id)
358 struct inode *inode = file->
f_path.dentry->d_inode;
359 struct fuse_conn *fc = get_fuse_conn(inode);
372 memset(&inarg, 0,
sizeof(inarg));
376 req->
in.h.nodeid = get_node_id(inode);
378 req->
in.args[0].size =
sizeof(inarg);
379 req->
in.args[0].value = &inarg;
382 err = req->
out.h.error;
400 static void fuse_sync_writes(
struct inode *inode)
407 int datasync,
int isdir)
409 struct inode *inode = file->
f_mapping->host;
410 struct fuse_conn *fc = get_fuse_conn(inode);
437 fuse_sync_writes(inode);
445 memset(&inarg, 0,
sizeof(inarg));
449 req->
in.h.nodeid = get_node_id(inode);
451 req->
in.args[0].size =
sizeof(inarg);
452 req->
in.args[0].value = &inarg;
454 err = req->
out.h.error;
468 static int fuse_fsync(
struct file *file, loff_t
start, loff_t
end,
475 size_t count,
int opcode)
488 req->
in.args[0].value = inarg;
490 req->
out.numargs = 1;
494 static size_t fuse_send_read(
struct fuse_req *req,
struct file *file,
508 return req->
out.args[0].size;
511 static void fuse_read_update_size(
struct inode *inode, loff_t
size,
514 struct fuse_conn *fc = get_fuse_conn(inode);
515 struct fuse_inode *fi = get_fuse_inode(inode);
517 spin_lock(&fc->
lock);
518 if (attr_ver == fi->
attr_version && size < inode->i_size) {
520 i_size_write(inode, size);
522 spin_unlock(&fc->
lock);
525 static int fuse_readpage(
struct file *file,
struct page *
page)
527 struct inode *inode = page->
mapping->host;
528 struct fuse_conn *fc = get_fuse_conn(inode);
545 fuse_wait_on_page_writeback(inode, page->
index);
554 req->
out.page_zeroing = 1;
555 req->
out.argpages = 1;
558 num_read = fuse_send_read(req, file, pos, count,
NULL);
559 err = req->
out.h.error;
566 if (num_read < count)
567 fuse_read_update_size(inode, pos + num_read, attr_ver);
569 SetPageUptodate(page);
581 size_t count = req->
misc.
read.in.size;
582 size_t num_read = req->
out.args[0].size;
586 mapping = req->
pages[i]->mapping;
589 struct inode *inode = mapping->
host;
594 if (!req->
out.h.error && num_read < count) {
598 fuse_read_update_size(inode, pos,
605 struct page *page = req->
pages[
i];
606 if (!req->
out.h.error)
607 SetPageUptodate(page);
614 fuse_file_put(req->
ff,
false);
617 static void fuse_send_readpages(
struct fuse_req *req,
struct file *file)
624 req->
out.argpages = 1;
625 req->
out.page_zeroing = 1;
626 req->
out.page_replace = 1;
631 req->
end = fuse_readpages_end;
635 fuse_readpages_end(fc, req);
646 static int fuse_readpages_fill(
void *_data,
struct page *page)
650 struct inode *inode = data->
inode;
651 struct fuse_conn *fc = get_fuse_conn(inode);
653 fuse_wait_on_page_writeback(inode, page->
index);
659 fuse_send_readpages(req, data->
file);
672 static int fuse_readpages(
struct file *file,
struct address_space *mapping,
675 struct inode *inode = mapping->
host;
676 struct fuse_conn *fc = get_fuse_conn(inode);
687 err = PTR_ERR(
data.req);
688 if (IS_ERR(
data.req))
693 if (
data.req->num_pages)
694 fuse_send_readpages(
data.req, file);
703 unsigned long nr_segs, loff_t pos)
705 struct inode *inode = iocb->
ki_filp->f_mapping->host;
706 struct fuse_conn *fc = get_fuse_conn(inode);
714 (pos + iov_length(iov, nr_segs) > i_size_read(inode))) {
725 loff_t pos,
size_t count)
736 if (ff->
fc->minor < 9)
740 req->
in.args[0].value = inarg;
742 req->
out.numargs = 1;
744 req->
out.args[0].value = outarg;
747 static size_t fuse_send_write(
struct fuse_req *req,
struct file *file,
754 fuse_write_fill(req, ff, pos, count);
766 struct fuse_conn *fc = get_fuse_conn(inode);
767 struct fuse_inode *fi = get_fuse_inode(inode);
769 spin_lock(&fc->
lock);
772 i_size_write(inode, pos);
773 spin_unlock(&fc->
lock);
776 static size_t fuse_send_write_pages(
struct fuse_req *req,
struct file *file,
777 struct inode *inode, loff_t pos,
785 fuse_wait_on_page_writeback(inode, req->
pages[i]->index);
787 res = fuse_send_write(req, file, pos, count,
NULL);
792 struct page *page = req->
pages[
i];
795 SetPageUptodate(page);
819 req->
in.argpages = 1;
841 if (mapping_writably_mapped(mapping))
871 }
while (iov_iter_count(ii) && count < fc->max_write &&
874 return count > 0 ? count :
err;
877 static ssize_t fuse_perform_write(
struct file *file,
881 struct inode *inode = mapping->
host;
882 struct fuse_conn *fc = get_fuse_conn(inode);
899 count = fuse_fill_write_pages(req, mapping, ii, pos);
905 num_written = fuse_send_write_pages(req, file, inode,
907 err = req->
out.h.error;
913 if (num_written != count)
918 }
while (!err && iov_iter_count(ii));
925 return res > 0 ? res :
err;
929 unsigned long nr_segs, loff_t pos)
931 struct file *file = iocb->
ki_filp;
937 struct inode *inode = mapping->
host;
950 sb_start_write(inode->
i_sb);
975 if (written < 0 || written == count)
981 iov_iter_init(&i, iov, nr_segs, count, written);
982 written_buffered = fuse_perform_write(file, mapping, &i, pos);
983 if (written_buffered < 0) {
984 err = written_buffered;
987 endbyte = pos + written_buffered - 1;
995 pos >> PAGE_CACHE_SHIFT,
996 endbyte >> PAGE_CACHE_SHIFT);
998 written += written_buffered;
999 iocb->
ki_pos = pos + written_buffered;
1001 iov_iter_init(&i, iov, nr_segs, count, 0);
1002 written = fuse_perform_write(file, mapping, &i, pos);
1004 iocb->
ki_pos = pos + written;
1009 sb_end_write(inode->
i_sb);
1011 return written ? written :
err;
1014 static void fuse_release_user_pages(
struct fuse_req *req,
int write)
1019 struct page *page = req->
pages[
i];
1026 static int fuse_get_user_pages(
struct fuse_req *req,
const char __user *
buf,
1027 size_t *nbytesp,
int write)
1029 size_t nbytes = *nbytesp;
1030 unsigned long user_addr = (
unsigned long) buf;
1031 unsigned offset = user_addr & ~
PAGE_MASK;
1037 req->
in.args[1].value = (
void *) user_addr;
1039 req->
out.args[0].value = (
void *) user_addr;
1055 req->
in.argpages = 1;
1057 req->
out.argpages = 1;
1060 *nbytesp =
min(*nbytesp, nbytes);
1066 size_t count, loff_t *ppos,
int write)
1077 return PTR_ERR(req);
1082 size_t nbytes =
min(count, nmax);
1083 int err = fuse_get_user_pages(req, buf, &nbytes, write);
1090 nres = fuse_send_write(req, file, pos, nbytes, owner);
1092 nres = fuse_send_read(req, file, pos, nbytes, owner);
1094 fuse_release_user_pages(req, !write);
1095 if (req->
out.h.error) {
1097 res = req->
out.h.error;
1099 }
else if (nres > nbytes) {
1125 static ssize_t fuse_direct_read(
struct file *file,
char __user *buf,
1126 size_t count, loff_t *ppos)
1129 struct inode *inode = file->
f_path.dentry->d_inode;
1141 static ssize_t __fuse_direct_write(
struct file *file,
const char __user *buf,
1142 size_t count, loff_t *ppos)
1144 struct inode *inode = file->
f_path.dentry->d_inode;
1159 static ssize_t fuse_direct_write(
struct file *file,
const char __user *buf,
1160 size_t count, loff_t *ppos)
1162 struct inode *inode = file->
f_path.dentry->d_inode;
1170 res = __fuse_direct_write(file, buf, count, ppos);
1179 fuse_file_put(req->
ff,
false);
1184 struct inode *inode = req->
inode;
1185 struct fuse_inode *fi = get_fuse_inode(inode);
1201 loff_t size = i_size_read(req->
inode);
1209 }
else if (inarg->
offset < size) {
1216 req->
in.args[1].size = inarg->
size;
1222 fuse_writepage_finish(fc, req);
1223 spin_unlock(&fc->
lock);
1224 fuse_writepage_free(fc, req);
1226 spin_lock(&fc->
lock);
1239 struct fuse_conn *fc = get_fuse_conn(inode);
1240 struct fuse_inode *fi = get_fuse_inode(inode);
1245 list_del_init(&req->
list);
1246 fuse_send_writepage(fc, req);
1252 struct inode *inode = req->
inode;
1253 struct fuse_inode *fi = get_fuse_inode(inode);
1256 spin_lock(&fc->
lock);
1258 fuse_writepage_finish(fc, req);
1259 spin_unlock(&fc->
lock);
1260 fuse_writepage_free(fc, req);
1263 static int fuse_writepage_locked(
struct page *page)
1266 struct inode *inode = mapping->
host;
1267 struct fuse_conn *fc = get_fuse_conn(inode);
1268 struct fuse_inode *fi = get_fuse_inode(inode);
1271 struct page *tmp_page;
1273 set_page_writeback(page);
1283 spin_lock(&fc->
lock);
1287 spin_unlock(&fc->
lock);
1291 copy_highpage(tmp_page, page);
1293 req->
in.argpages = 1;
1295 req->
pages[0] = tmp_page;
1297 req->
end = fuse_writepage_end;
1304 spin_lock(&fc->
lock);
1308 spin_unlock(&fc->
lock);
1323 err = fuse_writepage_locked(page);
1329 static int fuse_launder_page(
struct page *page)
1333 struct inode *inode = page->
mapping->host;
1334 err = fuse_writepage_locked(page);
1336 fuse_wait_on_page_writeback(inode, page->
index);
1365 static int fuse_page_mkwrite(
struct vm_area_struct *vma,
struct vm_fault *vmf)
1367 struct page *page = vmf->page;
1372 struct inode *inode = vma->
vm_file->f_mapping->host;
1374 fuse_wait_on_page_writeback(inode, page->
index);
1378 static const struct vm_operations_struct fuse_file_vm_ops = {
1379 .close = fuse_vma_close,
1381 .page_mkwrite = fuse_page_mkwrite,
1385 static int fuse_file_mmap(
struct file *file,
struct vm_area_struct *vma)
1388 struct inode *inode = file->f_dentry->d_inode;
1389 struct fuse_conn *fc = get_fuse_conn(inode);
1390 struct fuse_inode *fi = get_fuse_inode(inode);
1396 spin_lock(&fc->
lock);
1399 spin_unlock(&fc->
lock);
1401 file_accessed(file);
1402 vma->
vm_ops = &fuse_file_vm_ops;
1406 static int fuse_direct_mmap(
struct file *file,
struct vm_area_struct *vma)
1417 static int convert_fuse_file_lock(
const struct fuse_file_lock *ffl,
1420 switch (ffl->
type) {
1442 static void fuse_lk_fill(
struct fuse_req *req,
struct file *file,
1446 struct inode *inode = file->
f_path.dentry->d_inode;
1447 struct fuse_conn *fc = get_fuse_conn(inode);
1460 req->
in.h.nodeid = get_node_id(inode);
1461 req->
in.numargs = 1;
1462 req->
in.args[0].size =
sizeof(*arg);
1463 req->
in.args[0].value =
arg;
1466 static int fuse_getlk(
struct file *file,
struct file_lock *fl)
1468 struct inode *inode = file->
f_path.dentry->d_inode;
1469 struct fuse_conn *fc = get_fuse_conn(inode);
1476 return PTR_ERR(req);
1478 fuse_lk_fill(req, file, fl,
FUSE_GETLK, 0, 0);
1479 req->
out.numargs = 1;
1480 req->
out.args[0].size =
sizeof(outarg);
1481 req->
out.args[0].value = &outarg;
1483 err = req->
out.h.error;
1486 err = convert_fuse_file_lock(&outarg.lk, fl);
1491 static int fuse_setlk(
struct file *file,
struct file_lock *fl,
int flock)
1493 struct inode *inode = file->
f_path.dentry->d_inode;
1494 struct fuse_conn *fc = get_fuse_conn(inode);
1511 return PTR_ERR(req);
1513 fuse_lk_fill(req, file, fl, opcode, pid, flock);
1515 err = req->
out.h.error;
1525 struct inode *inode = file->
f_path.dentry->d_inode;
1526 struct fuse_conn *fc = get_fuse_conn(inode);
1536 err = fuse_getlk(file, fl);
1541 err = fuse_setlk(file, fl, 0);
1546 static int fuse_file_flock(
struct file *file,
int cmd,
struct file_lock *fl)
1548 struct inode *inode = file->
f_path.dentry->d_inode;
1549 struct fuse_conn *fc = get_fuse_conn(inode);
1560 err = fuse_setlk(file, fl, 1);
1568 struct inode *inode = mapping->
host;
1569 struct fuse_conn *fc = get_fuse_conn(inode);
1582 memset(&inarg, 0,
sizeof(inarg));
1583 inarg.block =
block;
1584 inarg.blocksize = inode->
i_sb->s_blocksize;
1586 req->
in.h.nodeid = get_node_id(inode);
1587 req->
in.numargs = 1;
1588 req->
in.args[0].size =
sizeof(inarg);
1589 req->
in.args[0].value = &inarg;
1590 req->
out.numargs = 1;
1591 req->
out.args[0].size =
sizeof(outarg);
1592 req->
out.args[0].value = &outarg;
1594 err = req->
out.h.error;
1599 return err ? 0 : outarg.block;
1602 static loff_t fuse_file_llseek(
struct file *file, loff_t offset,
int origin)
1605 struct inode *inode = file->
f_path.dentry->d_inode;
1620 static int fuse_ioctl_copy_user(
struct page **pages,
struct iovec *iov,
1621 unsigned int nr_segs,
size_t bytes,
bool to_user)
1629 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
1631 while (iov_iter_count(&ii)) {
1632 struct page *page = pages[page_idx++];
1641 size_t copy =
min(todo, iov_len);
1669 static int fuse_copy_ioctl_iovec_old(
struct iovec *
dst,
void *
src,
1670 size_t transferred,
unsigned count,
1673 #ifdef CONFIG_COMPAT
1674 if (count *
sizeof(
struct compat_iovec) == transferred) {
1675 struct compat_iovec *ciov =
src;
1686 for (i = 0; i <
count; i++) {
1687 dst[
i].
iov_base = compat_ptr(ciov[i].iov_base);
1694 if (count *
sizeof(
struct iovec) != transferred)
1697 memcpy(dst, src, transferred);
1702 static int fuse_verify_ioctl_iov(
struct iovec *iov,
size_t count)
1707 for (n = 0; n <
count; n++, iov++) {
1708 if (iov->
iov_len > (
size_t) max)
1715 static int fuse_copy_ioctl_iovec(
struct fuse_conn *fc,
struct iovec *dst,
1716 void *src,
size_t transferred,
unsigned count,
1722 if (fc->
minor < 16) {
1723 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
1730 for (i = 0; i <
count; i++) {
1732 if (fiov[i].
base != (
unsigned long) fiov[i].
base ||
1733 fiov[i].
len != (
unsigned long) fiov[i].
len)
1739 #ifdef CONFIG_COMPAT
1741 (ptr_to_compat(dst[i].iov_base) != fiov[
i].
base ||
1810 struct page **pages =
NULL;
1813 unsigned int in_iovs = 0, out_iovs = 0,
num_pages = 0, max_pages;
1814 size_t in_size, out_size, transferred;
1817 #if BITS_PER_LONG == 32
1830 if (!pages || !iov_page)
1838 struct iovec *iov = iov_page;
1855 inarg.
in_size = in_size = iov_length(in_iov, in_iovs);
1856 inarg.
out_size = out_size = iov_length(out_iov, out_iovs);
1888 req->
in.numargs = 1;
1889 req->
in.args[0].size =
sizeof(inarg);
1890 req->
in.args[0].value = &inarg;
1893 req->
in.args[1].size = in_size;
1894 req->
in.argpages = 1;
1896 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
1902 req->
out.numargs = 2;
1903 req->
out.args[0].size =
sizeof(outarg);
1904 req->
out.args[0].value = &outarg;
1905 req->
out.args[1].size = out_size;
1906 req->
out.argpages = 1;
1907 req->
out.argvar = 1;
1910 err = req->
out.h.error;
1911 transferred = req->
out.args[1].size;
1923 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
1940 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
1941 transferred, in_iovs + out_iovs,
1942 (flags & FUSE_IOCTL_COMPAT) != 0);
1948 out_iov = in_iov + in_iovs;
1950 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
1954 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
1965 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred,
true);
1974 return err ? err : outarg.
result;
1979 unsigned long arg,
unsigned int flags)
1981 struct inode *inode = file->f_dentry->d_inode;
1982 struct fuse_conn *fc = get_fuse_conn(inode);
1993 static long fuse_file_ioctl(
struct file *file,
unsigned int cmd,
1999 static long fuse_file_compat_ioctl(
struct file *file,
unsigned int cmd,
2024 else if (kh > ff->
kh)
2041 static void fuse_register_polled_file(
struct fuse_conn *fc,
2044 spin_lock(&fc->
lock);
2048 link = fuse_find_polled_node(fc, ff->
kh, &parent);
2053 spin_unlock(&fc->
lock);
2076 fuse_register_polled_file(fc, ff);
2085 req->
in.numargs = 1;
2086 req->
in.args[0].size =
sizeof(inarg);
2087 req->
in.args[0].value = &inarg;
2088 req->
out.numargs = 1;
2089 req->
out.args[0].size =
sizeof(outarg);
2090 req->
out.args[0].value = &outarg;
2092 err = req->
out.h.error;
2112 u64 kh = outarg->
kh;
2115 spin_lock(&fc->
lock);
2117 link = fuse_find_polled_node(fc, kh,
NULL);
2125 spin_unlock(&fc->
lock);
2129 static ssize_t fuse_loop_dio(
struct file *filp,
const struct iovec *iov,
2130 unsigned long nr_segs, loff_t *ppos,
int rw)
2135 while (nr_segs > 0) {
2146 nr = __fuse_direct_write(filp, base, len, ppos);
2148 nr = fuse_direct_read(filp, base, len, ppos);
2165 fuse_direct_IO(
int rw,
struct kiocb *iocb,
const struct iovec *iov,
2166 loff_t offset,
unsigned long nr_segs)
2169 struct file *file =
NULL;
2175 ret = fuse_loop_dio(file, iov, nr_segs, &pos, rw);
2199 return PTR_ERR(req);
2203 req->
in.numargs = 1;
2204 req->
in.args[0].size =
sizeof(inarg);
2205 req->
in.args[0].value = &inarg;
2207 err = req->
out.h.error;
2219 .llseek = fuse_file_llseek,
2221 .aio_read = fuse_file_aio_read,
2223 .aio_write = fuse_file_aio_write,
2224 .mmap = fuse_file_mmap,
2226 .flush = fuse_flush,
2227 .release = fuse_release,
2228 .fsync = fuse_fsync,
2230 .flock = fuse_file_flock,
2232 .unlocked_ioctl = fuse_file_ioctl,
2233 .compat_ioctl = fuse_file_compat_ioctl,
2238 static const struct file_operations fuse_direct_io_file_operations = {
2239 .llseek = fuse_file_llseek,
2240 .read = fuse_direct_read,
2241 .write = fuse_direct_write,
2242 .mmap = fuse_direct_mmap,
2244 .flush = fuse_flush,
2245 .release = fuse_release,
2246 .fsync = fuse_fsync,
2248 .flock = fuse_file_flock,
2249 .unlocked_ioctl = fuse_file_ioctl,
2250 .compat_ioctl = fuse_file_compat_ioctl,
2257 .readpage = fuse_readpage,
2258 .writepage = fuse_writepage,
2259 .launder_page = fuse_launder_page,
2260 .readpages = fuse_readpages,
2263 .direct_IO = fuse_direct_IO,
2268 inode->
i_fop = &fuse_file_operations;
2269 inode->
i_data.a_ops = &fuse_file_aops;