libnvdimm, namespace: fix pmem namespace leak, delete when size set to zero
[linux-2.6/btrfs-unstable.git] / fs / nfs / io.c
blob1fc5d1ce327e272c0878e8a53aa45b2dc96e97ad
1 /*
2 * Copyright (c) 2016 Trond Myklebust
4 * I/O and data path helper functionality.
5 */
7 #include <linux/types.h>
8 #include <linux/kernel.h>
9 #include <linux/bitops.h>
10 #include <linux/rwsem.h>
11 #include <linux/fs.h>
12 #include <linux/nfs_fs.h>
14 #include "internal.h"
16 /* Call with exclusively locked inode->i_rwsem */
17 static void nfs_block_o_direct(struct nfs_inode *nfsi, struct inode *inode)
19 if (test_bit(NFS_INO_ODIRECT, &nfsi->flags)) {
20 clear_bit(NFS_INO_ODIRECT, &nfsi->flags);
21 inode_dio_wait(inode);
25 /**
26 * nfs_start_io_read - declare the file is being used for buffered reads
27 * @inode - file inode
29 * Declare that a buffered read operation is about to start, and ensure
30 * that we block all direct I/O.
31 * On exit, the function ensures that the NFS_INO_ODIRECT flag is unset,
32 * and holds a shared lock on inode->i_rwsem to ensure that the flag
33 * cannot be changed.
34 * In practice, this means that buffered read operations are allowed to
35 * execute in parallel, thanks to the shared lock, whereas direct I/O
36 * operations need to wait to grab an exclusive lock in order to set
37 * NFS_INO_ODIRECT.
38 * Note that buffered writes and truncates both take a write lock on
39 * inode->i_rwsem, meaning that those are serialised w.r.t. the reads.
41 void
42 nfs_start_io_read(struct inode *inode)
44 struct nfs_inode *nfsi = NFS_I(inode);
45 /* Be an optimist! */
46 down_read(&inode->i_rwsem);
47 if (test_bit(NFS_INO_ODIRECT, &nfsi->flags) == 0)
48 return;
49 up_read(&inode->i_rwsem);
50 /* Slow path.... */
51 down_write(&inode->i_rwsem);
52 nfs_block_o_direct(nfsi, inode);
53 downgrade_write(&inode->i_rwsem);
56 /**
57 * nfs_end_io_read - declare that the buffered read operation is done
58 * @inode - file inode
60 * Declare that a buffered read operation is done, and release the shared
61 * lock on inode->i_rwsem.
63 void
64 nfs_end_io_read(struct inode *inode)
66 up_read(&inode->i_rwsem);
69 /**
70 * nfs_start_io_write - declare the file is being used for buffered writes
71 * @inode - file inode
73 * Declare that a buffered read operation is about to start, and ensure
74 * that we block all direct I/O.
76 void
77 nfs_start_io_write(struct inode *inode)
79 down_write(&inode->i_rwsem);
80 nfs_block_o_direct(NFS_I(inode), inode);
83 /**
84 * nfs_end_io_write - declare that the buffered write operation is done
85 * @inode - file inode
87 * Declare that a buffered write operation is done, and release the
88 * lock on inode->i_rwsem.
90 void
91 nfs_end_io_write(struct inode *inode)
93 up_write(&inode->i_rwsem);
96 /* Call with exclusively locked inode->i_rwsem */
97 static void nfs_block_buffered(struct nfs_inode *nfsi, struct inode *inode)
99 if (!test_bit(NFS_INO_ODIRECT, &nfsi->flags)) {
100 set_bit(NFS_INO_ODIRECT, &nfsi->flags);
101 nfs_wb_all(inode);
106 * nfs_end_io_direct - declare the file is being used for direct i/o
107 * @inode - file inode
109 * Declare that a direct I/O operation is about to start, and ensure
110 * that we block all buffered I/O.
111 * On exit, the function ensures that the NFS_INO_ODIRECT flag is set,
112 * and holds a shared lock on inode->i_rwsem to ensure that the flag
113 * cannot be changed.
114 * In practice, this means that direct I/O operations are allowed to
115 * execute in parallel, thanks to the shared lock, whereas buffered I/O
116 * operations need to wait to grab an exclusive lock in order to clear
117 * NFS_INO_ODIRECT.
118 * Note that buffered writes and truncates both take a write lock on
119 * inode->i_rwsem, meaning that those are serialised w.r.t. O_DIRECT.
121 void
122 nfs_start_io_direct(struct inode *inode)
124 struct nfs_inode *nfsi = NFS_I(inode);
125 /* Be an optimist! */
126 down_read(&inode->i_rwsem);
127 if (test_bit(NFS_INO_ODIRECT, &nfsi->flags) != 0)
128 return;
129 up_read(&inode->i_rwsem);
130 /* Slow path.... */
131 down_write(&inode->i_rwsem);
132 nfs_block_buffered(nfsi, inode);
133 downgrade_write(&inode->i_rwsem);
137 * nfs_end_io_direct - declare that the direct i/o operation is done
138 * @inode - file inode
140 * Declare that a direct I/O operation is done, and release the shared
141 * lock on inode->i_rwsem.
143 void
144 nfs_end_io_direct(struct inode *inode)
146 up_read(&inode->i_rwsem);