blob: dafa03ef0107302ec3b6e5a5eb1b28b1622f91d9 [file] [log] [blame]
Mike Marshall5db11c22015-07-17 10:38:12 -04001/*
2 * (C) 2001 Clemson University and The University of Chicago
3 *
4 * See COPYING in top-level directory.
5 */
6
7/*
8 * Linux VFS file operations.
9 */
10
11#include "protocol.h"
Mike Marshall575e9462015-12-04 12:56:14 -050012#include "orangefs-kernel.h"
13#include "orangefs-bufmap.h"
Mike Marshall5db11c22015-07-17 10:38:12 -040014#include <linux/fs.h>
15#include <linux/pagemap.h>
16
Mike Marshall5db11c22015-07-17 10:38:12 -040017/*
18 * Copy to client-core's address space from the buffers specified
19 * by the iovec upto total_size bytes.
20 * NOTE: the iovector can either contain addresses which
21 * can futher be kernel-space or user-space addresses.
22 * or it can pointers to struct page's
23 */
Yi Liu8bb8aef2015-11-24 15:12:14 -050024static int precopy_buffers(struct orangefs_bufmap *bufmap,
Mike Marshall5db11c22015-07-17 10:38:12 -040025 int buffer_index,
Al Viroa5c126a2015-10-08 17:54:31 -040026 struct iov_iter *iter,
Mike Marshall4d1c4402015-09-04 10:31:16 -040027 size_t total_size)
Mike Marshall5db11c22015-07-17 10:38:12 -040028{
29 int ret = 0;
Mike Marshall5db11c22015-07-17 10:38:12 -040030 /*
31 * copy data from application/kernel by pulling it out
32 * of the iovec.
33 */
Mike Marshall4d1c4402015-09-04 10:31:16 -040034
35
36 if (total_size) {
Yi Liu8bb8aef2015-11-24 15:12:14 -050037 ret = orangefs_bufmap_copy_from_iovec(bufmap,
38 iter,
39 buffer_index,
40 total_size);
Mike Marshall4d1c4402015-09-04 10:31:16 -040041 if (ret < 0)
42 gossip_err("%s: Failed to copy-in buffers. Please make sure that the pvfs2-client is running. %ld\n",
43 __func__,
44 (long)ret);
Mike Marshall4d1c4402015-09-04 10:31:16 -040045 }
46
Mike Marshall5db11c22015-07-17 10:38:12 -040047 if (ret < 0)
48 gossip_err("%s: Failed to copy-in buffers. Please make sure that the pvfs2-client is running. %ld\n",
49 __func__,
50 (long)ret);
51 return ret;
52}
53
54/*
55 * Copy from client-core's address space to the buffers specified
56 * by the iovec upto total_size bytes.
57 * NOTE: the iovector can either contain addresses which
58 * can futher be kernel-space or user-space addresses.
59 * or it can pointers to struct page's
60 */
Yi Liu8bb8aef2015-11-24 15:12:14 -050061static int postcopy_buffers(struct orangefs_bufmap *bufmap,
Mike Marshall5db11c22015-07-17 10:38:12 -040062 int buffer_index,
Al Viro5f0e3c92015-10-08 17:52:44 -040063 struct iov_iter *iter,
Mike Marshall4d1c4402015-09-04 10:31:16 -040064 size_t total_size)
Mike Marshall5db11c22015-07-17 10:38:12 -040065{
66 int ret = 0;
Mike Marshall5db11c22015-07-17 10:38:12 -040067 /*
68 * copy data to application/kernel by pushing it out to
69 * the iovec. NOTE; target buffers can be addresses or
70 * struct page pointers.
71 */
72 if (total_size) {
Yi Liu8bb8aef2015-11-24 15:12:14 -050073 ret = orangefs_bufmap_copy_to_iovec(bufmap,
74 iter,
75 buffer_index,
76 total_size);
Mike Marshall5db11c22015-07-17 10:38:12 -040077 if (ret < 0)
Mike Marshall4d1c4402015-09-04 10:31:16 -040078 gossip_err("%s: Failed to copy-out buffers. Please make sure that the pvfs2-client is running (%ld)\n",
Mike Marshall5db11c22015-07-17 10:38:12 -040079 __func__,
80 (long)ret);
81 }
82 return ret;
83}
84
85/*
86 * Post and wait for the I/O upcall to finish
87 */
Yi Liu8bb8aef2015-11-24 15:12:14 -050088static ssize_t wait_for_direct_io(enum ORANGEFS_io_type type, struct inode *inode,
Al Viro3c2fcfc2015-10-08 18:00:26 -040089 loff_t *offset, struct iov_iter *iter,
Mike Marshall4d1c4402015-09-04 10:31:16 -040090 size_t total_size, loff_t readahead_size)
Mike Marshall5db11c22015-07-17 10:38:12 -040091{
Yi Liu8bb8aef2015-11-24 15:12:14 -050092 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
93 struct orangefs_khandle *handle = &orangefs_inode->refn.khandle;
94 struct orangefs_bufmap *bufmap = NULL;
95 struct orangefs_kernel_op_s *new_op = NULL;
Al Viro7b9761a2016-02-07 01:25:06 -050096 struct iov_iter saved = *iter;
Mike Marshall5db11c22015-07-17 10:38:12 -040097 int buffer_index = -1;
98 ssize_t ret;
99
Yi Liu8bb8aef2015-11-24 15:12:14 -0500100 new_op = op_alloc(ORANGEFS_VFS_OP_FILE_IO);
Al Viroed42fe02016-01-22 19:47:47 -0500101 if (!new_op)
102 return -ENOMEM;
103
Mike Marshall5db11c22015-07-17 10:38:12 -0400104 /* synchronous I/O */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500105 new_op->upcall.req.io.async_vfs_io = ORANGEFS_VFS_SYNC_IO;
Mike Marshall5db11c22015-07-17 10:38:12 -0400106 new_op->upcall.req.io.readahead_size = readahead_size;
107 new_op->upcall.req.io.io_type = type;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500108 new_op->upcall.req.io.refn = orangefs_inode->refn;
Mike Marshall5db11c22015-07-17 10:38:12 -0400109
110populate_shared_memory:
111 /* get a shared buffer index */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500112 ret = orangefs_bufmap_get(&bufmap, &buffer_index);
Mike Marshall5db11c22015-07-17 10:38:12 -0400113 if (ret < 0) {
114 gossip_debug(GOSSIP_FILE_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500115 "%s: orangefs_bufmap_get failure (%ld)\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400116 __func__, (long)ret);
117 goto out;
118 }
119 gossip_debug(GOSSIP_FILE_DEBUG,
120 "%s(%pU): GET op %p -> buffer_index %d\n",
121 __func__,
122 handle,
123 new_op,
124 buffer_index);
125
126 new_op->uses_shared_memory = 1;
127 new_op->upcall.req.io.buf_index = buffer_index;
128 new_op->upcall.req.io.count = total_size;
129 new_op->upcall.req.io.offset = *offset;
130
131 gossip_debug(GOSSIP_FILE_DEBUG,
Al Viro3c2fcfc2015-10-08 18:00:26 -0400132 "%s(%pU): offset: %llu total_size: %zd\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400133 __func__,
134 handle,
Mike Marshall5db11c22015-07-17 10:38:12 -0400135 llu(*offset),
136 total_size);
137 /*
138 * Stage 1: copy the buffers into client-core's address space
139 * precopy_buffers only pertains to writes.
140 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500141 if (type == ORANGEFS_IO_WRITE) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400142 ret = precopy_buffers(bufmap,
143 buffer_index,
Al Viro3c2fcfc2015-10-08 18:00:26 -0400144 iter,
Mike Marshall4d1c4402015-09-04 10:31:16 -0400145 total_size);
Mike Marshall5db11c22015-07-17 10:38:12 -0400146 if (ret < 0)
147 goto out;
148 }
149
150 gossip_debug(GOSSIP_FILE_DEBUG,
151 "%s(%pU): Calling post_io_request with tag (%llu)\n",
152 __func__,
153 handle,
154 llu(new_op->tag));
155
156 /* Stage 2: Service the I/O operation */
157 ret = service_operation(new_op,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500158 type == ORANGEFS_IO_WRITE ?
Mike Marshall5db11c22015-07-17 10:38:12 -0400159 "file_write" :
160 "file_read",
161 get_interruptible_flag(inode));
162
163 /*
164 * If service_operation() returns -EAGAIN #and# the operation was
Yi Liu8bb8aef2015-11-24 15:12:14 -0500165 * purged from orangefs_request_list or htable_ops_in_progress, then
Mike Marshall5db11c22015-07-17 10:38:12 -0400166 * we know that the client was restarted, causing the shared memory
167 * area to be wiped clean. To restart a write operation in this
168 * case, we must re-copy the data from the user's iovec to a NEW
169 * shared memory location. To restart a read operation, we must get
170 * a new shared memory location.
171 */
172 if (ret == -EAGAIN && op_state_purged(new_op)) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500173 orangefs_bufmap_put(bufmap, buffer_index);
Al Viroe17be9f2016-02-06 14:59:38 -0500174 buffer_index = -1;
Al Viro7b9761a2016-02-07 01:25:06 -0500175 if (type == ORANGEFS_IO_WRITE)
176 *iter = saved;
Mike Marshall5db11c22015-07-17 10:38:12 -0400177 gossip_debug(GOSSIP_FILE_DEBUG,
178 "%s:going to repopulate_shared_memory.\n",
179 __func__);
180 goto populate_shared_memory;
181 }
182
183 if (ret < 0) {
Al Viroc0eae8c2016-02-11 21:28:52 -0500184 /*
185 * XXX: needs to be optimized - we only need to cancel if it
186 * had been seen by daemon and not completed
187 */
188 if (!op_state_serviced(new_op)) {
189 orangefs_cancel_op_in_progress(new_op->tag);
190 } else {
191 complete(&new_op->done);
192 }
193 orangefs_bufmap_put(bufmap, buffer_index);
194 buffer_index = -1;
Mike Marshall5db11c22015-07-17 10:38:12 -0400195 /*
Mike Marshall54804942015-10-05 13:44:24 -0400196 * don't write an error to syslog on signaled operation
197 * termination unless we've got debugging turned on, as
198 * this can happen regularly (i.e. ctrl-c)
Mike Marshall5db11c22015-07-17 10:38:12 -0400199 */
200 if (ret == -EINTR)
201 gossip_debug(GOSSIP_FILE_DEBUG,
202 "%s: returning error %ld\n", __func__,
203 (long)ret);
204 else
205 gossip_err("%s: error in %s handle %pU, returning %zd\n",
206 __func__,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500207 type == ORANGEFS_IO_READ ?
Mike Marshall5db11c22015-07-17 10:38:12 -0400208 "read from" : "write to",
209 handle, ret);
210 goto out;
211 }
212
213 /*
214 * Stage 3: Post copy buffers from client-core's address space
215 * postcopy_buffers only pertains to reads.
216 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500217 if (type == ORANGEFS_IO_READ) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400218 ret = postcopy_buffers(bufmap,
219 buffer_index,
Al Viro3c2fcfc2015-10-08 18:00:26 -0400220 iter,
Mike Marshall4d1c4402015-09-04 10:31:16 -0400221 new_op->downcall.resp.io.amt_complete);
Al Viroc0eae8c2016-02-11 21:28:52 -0500222 if (ret < 0)
223 goto done_copying;
Mike Marshall5db11c22015-07-17 10:38:12 -0400224 }
225 gossip_debug(GOSSIP_FILE_DEBUG,
226 "%s(%pU): Amount written as returned by the sys-io call:%d\n",
227 __func__,
228 handle,
229 (int)new_op->downcall.resp.io.amt_complete);
230
231 ret = new_op->downcall.resp.io.amt_complete;
232
Al Viroc0eae8c2016-02-11 21:28:52 -0500233done_copying:
Mike Marshall5db11c22015-07-17 10:38:12 -0400234 /*
Mike Marshall54804942015-10-05 13:44:24 -0400235 * tell the device file owner waiting on I/O that this read has
Al Viroed42fe02016-01-22 19:47:47 -0500236 * completed and it can return now.
Mike Marshall5db11c22015-07-17 10:38:12 -0400237 */
Al Virob0bc3a72016-01-23 13:50:37 -0500238 complete(&new_op->done);
Mike Marshall5db11c22015-07-17 10:38:12 -0400239
240out:
241 if (buffer_index >= 0) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500242 orangefs_bufmap_put(bufmap, buffer_index);
Mike Marshall5db11c22015-07-17 10:38:12 -0400243 gossip_debug(GOSSIP_FILE_DEBUG,
244 "%s(%pU): PUT buffer_index %d\n",
245 __func__, handle, buffer_index);
246 buffer_index = -1;
247 }
Al Viroed42fe02016-01-22 19:47:47 -0500248 op_release(new_op);
Mike Marshall5db11c22015-07-17 10:38:12 -0400249 return ret;
250}
251
252/*
Mike Marshall5db11c22015-07-17 10:38:12 -0400253 * Common entry point for read/write/readv/writev
254 * This function will dispatch it to either the direct I/O
255 * or buffered I/O path depending on the mount options and/or
256 * augmented/extended metadata attached to the file.
257 * Note: File extended attributes override any mount options.
258 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500259static ssize_t do_readv_writev(enum ORANGEFS_io_type type, struct file *file,
Al Viro0071ed12015-10-08 18:22:08 -0400260 loff_t *offset, struct iov_iter *iter)
Mike Marshall5db11c22015-07-17 10:38:12 -0400261{
262 struct inode *inode = file->f_mapping->host;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500263 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
264 struct orangefs_khandle *handle = &orangefs_inode->refn.khandle;
Al Viro0071ed12015-10-08 18:22:08 -0400265 size_t count = iov_iter_count(iter);
Al Virodc4067f2015-10-08 18:17:26 -0400266 ssize_t total_count = 0;
267 ssize_t ret = -EINVAL;
Mike Marshall5db11c22015-07-17 10:38:12 -0400268
269 gossip_debug(GOSSIP_FILE_DEBUG,
270 "%s-BEGIN(%pU): count(%d) after estimate_max_iovecs.\n",
271 __func__,
272 handle,
273 (int)count);
274
Yi Liu8bb8aef2015-11-24 15:12:14 -0500275 if (type == ORANGEFS_IO_WRITE) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400276 gossip_debug(GOSSIP_FILE_DEBUG,
277 "%s(%pU): proceeding with offset : %llu, "
278 "size %d\n",
279 __func__,
280 handle,
281 llu(*offset),
282 (int)count);
283 }
284
285 if (count == 0) {
286 ret = 0;
287 goto out;
288 }
289
Al Viro0071ed12015-10-08 18:22:08 -0400290 while (iov_iter_count(iter)) {
291 size_t each_count = iov_iter_count(iter);
Mike Marshall5db11c22015-07-17 10:38:12 -0400292 size_t amt_complete;
293
294 /* how much to transfer in this loop iteration */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500295 if (each_count > orangefs_bufmap_size_query())
296 each_count = orangefs_bufmap_size_query();
Mike Marshall5db11c22015-07-17 10:38:12 -0400297
298 gossip_debug(GOSSIP_FILE_DEBUG,
299 "%s(%pU): size of each_count(%d)\n",
300 __func__,
301 handle,
302 (int)each_count);
303 gossip_debug(GOSSIP_FILE_DEBUG,
304 "%s(%pU): BEFORE wait_for_io: offset is %d\n",
305 __func__,
306 handle,
307 (int)*offset);
308
Al Viro0071ed12015-10-08 18:22:08 -0400309 ret = wait_for_direct_io(type, inode, offset, iter,
Al Viro3c2fcfc2015-10-08 18:00:26 -0400310 each_count, 0);
Mike Marshall5db11c22015-07-17 10:38:12 -0400311 gossip_debug(GOSSIP_FILE_DEBUG,
312 "%s(%pU): return from wait_for_io:%d\n",
313 __func__,
314 handle,
315 (int)ret);
316
317 if (ret < 0)
318 goto out;
319
Mike Marshall5db11c22015-07-17 10:38:12 -0400320 *offset += ret;
321 total_count += ret;
322 amt_complete = ret;
323
324 gossip_debug(GOSSIP_FILE_DEBUG,
325 "%s(%pU): AFTER wait_for_io: offset is %d\n",
326 __func__,
327 handle,
328 (int)*offset);
329
330 /*
331 * if we got a short I/O operations,
332 * fall out and return what we got so far
333 */
334 if (amt_complete < each_count)
335 break;
336 } /*end while */
337
338 if (total_count > 0)
339 ret = total_count;
340out:
Mike Marshall5db11c22015-07-17 10:38:12 -0400341 if (ret > 0) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500342 if (type == ORANGEFS_IO_READ) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400343 file_accessed(file);
344 } else {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500345 SetMtimeFlag(orangefs_inode);
Mike Marshall5db11c22015-07-17 10:38:12 -0400346 inode->i_mtime = CURRENT_TIME;
347 mark_inode_dirty_sync(inode);
348 }
349 }
350
351 gossip_debug(GOSSIP_FILE_DEBUG,
352 "%s(%pU): Value(%d) returned.\n",
353 __func__,
354 handle,
355 (int)ret);
356
357 return ret;
358}
359
360/*
361 * Read data from a specified offset in a file (referenced by inode).
362 * Data may be placed either in a user or kernel buffer.
363 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500364ssize_t orangefs_inode_read(struct inode *inode,
365 struct iov_iter *iter,
366 loff_t *offset,
367 loff_t readahead_size)
Mike Marshall5db11c22015-07-17 10:38:12 -0400368{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500369 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
Al Viro74f68fc2015-10-08 18:31:05 -0400370 size_t count = iov_iter_count(iter);
Mike Marshall5db11c22015-07-17 10:38:12 -0400371 size_t bufmap_size;
Mike Marshall5db11c22015-07-17 10:38:12 -0400372 ssize_t ret = -EINVAL;
373
Yi Liu8bb8aef2015-11-24 15:12:14 -0500374 g_orangefs_stats.reads++;
Mike Marshall5db11c22015-07-17 10:38:12 -0400375
Yi Liu8bb8aef2015-11-24 15:12:14 -0500376 bufmap_size = orangefs_bufmap_size_query();
Mike Marshall5db11c22015-07-17 10:38:12 -0400377 if (count > bufmap_size) {
378 gossip_debug(GOSSIP_FILE_DEBUG,
379 "%s: count is too large (%zd/%zd)!\n",
380 __func__, count, bufmap_size);
381 return -EINVAL;
382 }
383
384 gossip_debug(GOSSIP_FILE_DEBUG,
385 "%s(%pU) %zd@%llu\n",
386 __func__,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500387 &orangefs_inode->refn.khandle,
Mike Marshall5db11c22015-07-17 10:38:12 -0400388 count,
389 llu(*offset));
390
Yi Liu8bb8aef2015-11-24 15:12:14 -0500391 ret = wait_for_direct_io(ORANGEFS_IO_READ, inode, offset, iter,
Mike Marshall4d1c4402015-09-04 10:31:16 -0400392 count, readahead_size);
Mike Marshall5db11c22015-07-17 10:38:12 -0400393 if (ret > 0)
394 *offset += ret;
395
396 gossip_debug(GOSSIP_FILE_DEBUG,
397 "%s(%pU): Value(%zd) returned.\n",
398 __func__,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500399 &orangefs_inode->refn.khandle,
Mike Marshall5db11c22015-07-17 10:38:12 -0400400 ret);
401
402 return ret;
403}
404
Yi Liu8bb8aef2015-11-24 15:12:14 -0500405static ssize_t orangefs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Mike Marshall5db11c22015-07-17 10:38:12 -0400406{
407 struct file *file = iocb->ki_filp;
408 loff_t pos = *(&iocb->ki_pos);
409 ssize_t rc = 0;
Mike Marshall5db11c22015-07-17 10:38:12 -0400410
411 BUG_ON(iocb->private);
412
Yi Liu8bb8aef2015-11-24 15:12:14 -0500413 gossip_debug(GOSSIP_FILE_DEBUG, "orangefs_file_read_iter\n");
Mike Marshall5db11c22015-07-17 10:38:12 -0400414
Yi Liu8bb8aef2015-11-24 15:12:14 -0500415 g_orangefs_stats.reads++;
Mike Marshall5db11c22015-07-17 10:38:12 -0400416
Yi Liu8bb8aef2015-11-24 15:12:14 -0500417 rc = do_readv_writev(ORANGEFS_IO_READ, file, &pos, iter);
Mike Marshall5db11c22015-07-17 10:38:12 -0400418 iocb->ki_pos = pos;
419
420 return rc;
421}
422
Yi Liu8bb8aef2015-11-24 15:12:14 -0500423static ssize_t orangefs_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
Mike Marshall5db11c22015-07-17 10:38:12 -0400424{
425 struct file *file = iocb->ki_filp;
Mike Marshall3f1b6942015-11-13 13:05:11 -0500426 loff_t pos;
Mike Marshall5db11c22015-07-17 10:38:12 -0400427 ssize_t rc;
428
429 BUG_ON(iocb->private);
430
Yi Liu8bb8aef2015-11-24 15:12:14 -0500431 gossip_debug(GOSSIP_FILE_DEBUG, "orangefs_file_write_iter\n");
Mike Marshall5db11c22015-07-17 10:38:12 -0400432
433 mutex_lock(&file->f_mapping->host->i_mutex);
434
435 /* Make sure generic_write_checks sees an up to date inode size. */
436 if (file->f_flags & O_APPEND) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500437 rc = orangefs_inode_getattr(file->f_mapping->host,
Martin Brandenburg99109822016-01-28 10:19:40 -0500438 ORANGEFS_ATTR_SYS_SIZE, 0);
Mike Marshall5db11c22015-07-17 10:38:12 -0400439 if (rc) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500440 gossip_err("%s: orangefs_inode_getattr failed, rc:%zd:.\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400441 __func__, rc);
442 goto out;
443 }
444 }
445
446 if (file->f_pos > i_size_read(file->f_mapping->host))
Yi Liu8bb8aef2015-11-24 15:12:14 -0500447 orangefs_i_size_write(file->f_mapping->host, file->f_pos);
Mike Marshall5db11c22015-07-17 10:38:12 -0400448
449 rc = generic_write_checks(iocb, iter);
450
451 if (rc <= 0) {
452 gossip_err("%s: generic_write_checks failed, rc:%zd:.\n",
453 __func__, rc);
454 goto out;
455 }
456
Mike Marshall3f1b6942015-11-13 13:05:11 -0500457 /*
458 * if we are appending, generic_write_checks would have updated
459 * pos to the end of the file, so we will wait till now to set
460 * pos...
461 */
462 pos = *(&iocb->ki_pos);
463
Yi Liu8bb8aef2015-11-24 15:12:14 -0500464 rc = do_readv_writev(ORANGEFS_IO_WRITE,
Mike Marshall5db11c22015-07-17 10:38:12 -0400465 file,
466 &pos,
Al Viro0071ed12015-10-08 18:22:08 -0400467 iter);
Mike Marshall5db11c22015-07-17 10:38:12 -0400468 if (rc < 0) {
469 gossip_err("%s: do_readv_writev failed, rc:%zd:.\n",
470 __func__, rc);
471 goto out;
472 }
473
474 iocb->ki_pos = pos;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500475 g_orangefs_stats.writes++;
Mike Marshall5db11c22015-07-17 10:38:12 -0400476
477out:
478
479 mutex_unlock(&file->f_mapping->host->i_mutex);
480 return rc;
481}
482
483/*
484 * Perform a miscellaneous operation on a file.
485 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500486static long orangefs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Mike Marshall5db11c22015-07-17 10:38:12 -0400487{
488 int ret = -ENOTTY;
489 __u64 val = 0;
490 unsigned long uval;
491
492 gossip_debug(GOSSIP_FILE_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500493 "orangefs_ioctl: called with cmd %d\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400494 cmd);
495
496 /*
497 * we understand some general ioctls on files, such as the immutable
498 * and append flags
499 */
500 if (cmd == FS_IOC_GETFLAGS) {
501 val = 0;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500502 ret = orangefs_inode_getxattr(file_inode(file),
503 ORANGEFS_XATTR_NAME_DEFAULT_PREFIX,
504 "user.pvfs2.meta_hint",
505 &val, sizeof(val));
Mike Marshall5db11c22015-07-17 10:38:12 -0400506 if (ret < 0 && ret != -ENODATA)
507 return ret;
508 else if (ret == -ENODATA)
509 val = 0;
510 uval = val;
511 gossip_debug(GOSSIP_FILE_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500512 "orangefs_ioctl: FS_IOC_GETFLAGS: %llu\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400513 (unsigned long long)uval);
514 return put_user(uval, (int __user *)arg);
515 } else if (cmd == FS_IOC_SETFLAGS) {
516 ret = 0;
517 if (get_user(uval, (int __user *)arg))
518 return -EFAULT;
519 /*
Yi Liu8bb8aef2015-11-24 15:12:14 -0500520 * ORANGEFS_MIRROR_FL is set internally when the mirroring mode
Mike Marshall5db11c22015-07-17 10:38:12 -0400521 * is turned on for a file. The user is not allowed to turn
522 * on this bit, but the bit is present if the user first gets
523 * the flags and then updates the flags with some new
524 * settings. So, we ignore it in the following edit. bligon.
525 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500526 if ((uval & ~ORANGEFS_MIRROR_FL) &
Mike Marshall5db11c22015-07-17 10:38:12 -0400527 (~(FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NOATIME_FL))) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500528 gossip_err("orangefs_ioctl: the FS_IOC_SETFLAGS only supports setting one of FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NOATIME_FL\n");
Mike Marshall5db11c22015-07-17 10:38:12 -0400529 return -EINVAL;
530 }
531 val = uval;
532 gossip_debug(GOSSIP_FILE_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500533 "orangefs_ioctl: FS_IOC_SETFLAGS: %llu\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400534 (unsigned long long)val);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500535 ret = orangefs_inode_setxattr(file_inode(file),
536 ORANGEFS_XATTR_NAME_DEFAULT_PREFIX,
537 "user.pvfs2.meta_hint",
538 &val, sizeof(val), 0);
Mike Marshall5db11c22015-07-17 10:38:12 -0400539 }
540
541 return ret;
542}
543
544/*
545 * Memory map a region of a file.
546 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500547static int orangefs_file_mmap(struct file *file, struct vm_area_struct *vma)
Mike Marshall5db11c22015-07-17 10:38:12 -0400548{
549 gossip_debug(GOSSIP_FILE_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500550 "orangefs_file_mmap: called on %s\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400551 (file ?
552 (char *)file->f_path.dentry->d_name.name :
553 (char *)"Unknown"));
554
555 /* set the sequential readahead hint */
556 vma->vm_flags |= VM_SEQ_READ;
557 vma->vm_flags &= ~VM_RAND_READ;
Martin Brandenburg35390802015-09-30 13:11:54 -0400558
559 /* Use readonly mmap since we cannot support writable maps. */
560 return generic_file_readonly_mmap(file, vma);
Mike Marshall5db11c22015-07-17 10:38:12 -0400561}
562
563#define mapping_nrpages(idata) ((idata)->nrpages)
564
565/*
566 * Called to notify the module that there are no more references to
567 * this file (i.e. no processes have it open).
568 *
569 * \note Not called when each file is closed.
570 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500571static int orangefs_file_release(struct inode *inode, struct file *file)
Mike Marshall5db11c22015-07-17 10:38:12 -0400572{
573 gossip_debug(GOSSIP_FILE_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500574 "orangefs_file_release: called on %s\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400575 file->f_path.dentry->d_name.name);
576
Yi Liu8bb8aef2015-11-24 15:12:14 -0500577 orangefs_flush_inode(inode);
Mike Marshall5db11c22015-07-17 10:38:12 -0400578
579 /*
Mike Marshall54804942015-10-05 13:44:24 -0400580 * remove all associated inode pages from the page cache and mmap
581 * readahead cache (if any); this forces an expensive refresh of
582 * data for the next caller of mmap (or 'get_block' accesses)
Mike Marshall5db11c22015-07-17 10:38:12 -0400583 */
584 if (file->f_path.dentry->d_inode &&
585 file->f_path.dentry->d_inode->i_mapping &&
586 mapping_nrpages(&file->f_path.dentry->d_inode->i_data))
587 truncate_inode_pages(file->f_path.dentry->d_inode->i_mapping,
588 0);
589 return 0;
590}
591
592/*
593 * Push all data for a specific file onto permanent storage.
594 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500595static int orangefs_fsync(struct file *file,
Mike Marshall84d02152015-07-28 13:27:51 -0400596 loff_t start,
597 loff_t end,
598 int datasync)
Mike Marshall5db11c22015-07-17 10:38:12 -0400599{
600 int ret = -EINVAL;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500601 struct orangefs_inode_s *orangefs_inode =
602 ORANGEFS_I(file->f_path.dentry->d_inode);
603 struct orangefs_kernel_op_s *new_op = NULL;
Mike Marshall5db11c22015-07-17 10:38:12 -0400604
605 /* required call */
606 filemap_write_and_wait_range(file->f_mapping, start, end);
607
Yi Liu8bb8aef2015-11-24 15:12:14 -0500608 new_op = op_alloc(ORANGEFS_VFS_OP_FSYNC);
Mike Marshall5db11c22015-07-17 10:38:12 -0400609 if (!new_op)
610 return -ENOMEM;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500611 new_op->upcall.req.fsync.refn = orangefs_inode->refn;
Mike Marshall5db11c22015-07-17 10:38:12 -0400612
613 ret = service_operation(new_op,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500614 "orangefs_fsync",
Mike Marshall5db11c22015-07-17 10:38:12 -0400615 get_interruptible_flag(file->f_path.dentry->d_inode));
616
617 gossip_debug(GOSSIP_FILE_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500618 "orangefs_fsync got return value of %d\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400619 ret);
620
621 op_release(new_op);
622
Yi Liu8bb8aef2015-11-24 15:12:14 -0500623 orangefs_flush_inode(file->f_path.dentry->d_inode);
Mike Marshall5db11c22015-07-17 10:38:12 -0400624 return ret;
625}
626
627/*
628 * Change the file pointer position for an instance of an open file.
629 *
630 * \note If .llseek is overriden, we must acquire lock as described in
631 * Documentation/filesystems/Locking.
632 *
633 * Future upgrade could support SEEK_DATA and SEEK_HOLE but would
634 * require much changes to the FS
635 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500636static loff_t orangefs_file_llseek(struct file *file, loff_t offset, int origin)
Mike Marshall5db11c22015-07-17 10:38:12 -0400637{
638 int ret = -EINVAL;
639 struct inode *inode = file->f_path.dentry->d_inode;
640
641 if (!inode) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500642 gossip_err("orangefs_file_llseek: invalid inode (NULL)\n");
Mike Marshall5db11c22015-07-17 10:38:12 -0400643 return ret;
644 }
645
Yi Liu8bb8aef2015-11-24 15:12:14 -0500646 if (origin == ORANGEFS_SEEK_END) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400647 /*
648 * revalidate the inode's file size.
649 * NOTE: We are only interested in file size here,
650 * so we set mask accordingly.
651 */
Martin Brandenburg99109822016-01-28 10:19:40 -0500652 ret = orangefs_inode_getattr(inode, ORANGEFS_ATTR_SYS_SIZE, 0);
Mike Marshall5db11c22015-07-17 10:38:12 -0400653 if (ret) {
654 gossip_debug(GOSSIP_FILE_DEBUG,
655 "%s:%s:%d calling make bad inode\n",
656 __FILE__,
657 __func__,
658 __LINE__);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500659 orangefs_make_bad_inode(inode);
Mike Marshall5db11c22015-07-17 10:38:12 -0400660 return ret;
661 }
662 }
663
664 gossip_debug(GOSSIP_FILE_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500665 "orangefs_file_llseek: offset is %ld | origin is %d"
Mike Marshall54804942015-10-05 13:44:24 -0400666 " | inode size is %lu\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400667 (long)offset,
668 origin,
669 (unsigned long)file->f_path.dentry->d_inode->i_size);
670
671 return generic_file_llseek(file, offset, origin);
672}
673
674/*
675 * Support local locks (locks that only this kernel knows about)
676 * if Orangefs was mounted -o local_lock.
677 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500678static int orangefs_lock(struct file *filp, int cmd, struct file_lock *fl)
Mike Marshall5db11c22015-07-17 10:38:12 -0400679{
Mike Marshallf957ae22015-09-24 12:53:05 -0400680 int rc = -EINVAL;
Mike Marshall5db11c22015-07-17 10:38:12 -0400681
Yi Liu8bb8aef2015-11-24 15:12:14 -0500682 if (ORANGEFS_SB(filp->f_inode->i_sb)->flags & ORANGEFS_OPT_LOCAL_LOCK) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400683 if (cmd == F_GETLK) {
684 rc = 0;
685 posix_test_lock(filp, fl);
686 } else {
687 rc = posix_lock_file(filp, fl, NULL);
688 }
689 }
690
691 return rc;
692}
693
Yi Liu8bb8aef2015-11-24 15:12:14 -0500694/** ORANGEFS implementation of VFS file operations */
695const struct file_operations orangefs_file_operations = {
696 .llseek = orangefs_file_llseek,
697 .read_iter = orangefs_file_read_iter,
698 .write_iter = orangefs_file_write_iter,
699 .lock = orangefs_lock,
700 .unlocked_ioctl = orangefs_ioctl,
701 .mmap = orangefs_file_mmap,
Mike Marshall5db11c22015-07-17 10:38:12 -0400702 .open = generic_file_open,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500703 .release = orangefs_file_release,
704 .fsync = orangefs_fsync,
Mike Marshall5db11c22015-07-17 10:38:12 -0400705};