blob: 3b1e9e83eb914aad19a7aaba50580c34036ffa78 [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)) {
Al Viro1357d062016-02-11 21:34:52 -0500173 orangefs_bufmap_put(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 /*
Mike Marshall54804942015-10-05 13:44:24 -0400185 * don't write an error to syslog on signaled operation
186 * termination unless we've got debugging turned on, as
187 * this can happen regularly (i.e. ctrl-c)
Mike Marshall5db11c22015-07-17 10:38:12 -0400188 */
189 if (ret == -EINTR)
190 gossip_debug(GOSSIP_FILE_DEBUG,
191 "%s: returning error %ld\n", __func__,
192 (long)ret);
193 else
194 gossip_err("%s: error in %s handle %pU, returning %zd\n",
195 __func__,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500196 type == ORANGEFS_IO_READ ?
Mike Marshall5db11c22015-07-17 10:38:12 -0400197 "read from" : "write to",
198 handle, ret);
Al Viro78699e22016-02-11 23:07:19 -0500199 if (orangefs_cancel_op_in_progress(new_op))
200 return ret;
201
202 goto done_copying;
Mike Marshall5db11c22015-07-17 10:38:12 -0400203 }
204
205 /*
206 * Stage 3: Post copy buffers from client-core's address space
207 * postcopy_buffers only pertains to reads.
208 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500209 if (type == ORANGEFS_IO_READ) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400210 ret = postcopy_buffers(bufmap,
211 buffer_index,
Al Viro3c2fcfc2015-10-08 18:00:26 -0400212 iter,
Mike Marshall4d1c4402015-09-04 10:31:16 -0400213 new_op->downcall.resp.io.amt_complete);
Al Viroc0eae8c2016-02-11 21:28:52 -0500214 if (ret < 0)
215 goto done_copying;
Mike Marshall5db11c22015-07-17 10:38:12 -0400216 }
217 gossip_debug(GOSSIP_FILE_DEBUG,
218 "%s(%pU): Amount written as returned by the sys-io call:%d\n",
219 __func__,
220 handle,
221 (int)new_op->downcall.resp.io.amt_complete);
222
223 ret = new_op->downcall.resp.io.amt_complete;
224
Al Viroc0eae8c2016-02-11 21:28:52 -0500225done_copying:
Mike Marshall5db11c22015-07-17 10:38:12 -0400226 /*
Mike Marshall54804942015-10-05 13:44:24 -0400227 * tell the device file owner waiting on I/O that this read has
Al Viroed42fe02016-01-22 19:47:47 -0500228 * completed and it can return now.
Mike Marshall5db11c22015-07-17 10:38:12 -0400229 */
Al Virob0bc3a72016-01-23 13:50:37 -0500230 complete(&new_op->done);
Mike Marshall5db11c22015-07-17 10:38:12 -0400231
232out:
233 if (buffer_index >= 0) {
Al Viro1357d062016-02-11 21:34:52 -0500234 orangefs_bufmap_put(buffer_index);
Mike Marshall5db11c22015-07-17 10:38:12 -0400235 gossip_debug(GOSSIP_FILE_DEBUG,
236 "%s(%pU): PUT buffer_index %d\n",
237 __func__, handle, buffer_index);
238 buffer_index = -1;
239 }
Al Viroed42fe02016-01-22 19:47:47 -0500240 op_release(new_op);
Mike Marshall5db11c22015-07-17 10:38:12 -0400241 return ret;
242}
243
244/*
Mike Marshall5db11c22015-07-17 10:38:12 -0400245 * Common entry point for read/write/readv/writev
246 * This function will dispatch it to either the direct I/O
247 * or buffered I/O path depending on the mount options and/or
248 * augmented/extended metadata attached to the file.
249 * Note: File extended attributes override any mount options.
250 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500251static ssize_t do_readv_writev(enum ORANGEFS_io_type type, struct file *file,
Al Viro0071ed12015-10-08 18:22:08 -0400252 loff_t *offset, struct iov_iter *iter)
Mike Marshall5db11c22015-07-17 10:38:12 -0400253{
254 struct inode *inode = file->f_mapping->host;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500255 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
256 struct orangefs_khandle *handle = &orangefs_inode->refn.khandle;
Al Viro0071ed12015-10-08 18:22:08 -0400257 size_t count = iov_iter_count(iter);
Al Virodc4067f2015-10-08 18:17:26 -0400258 ssize_t total_count = 0;
259 ssize_t ret = -EINVAL;
Mike Marshall5db11c22015-07-17 10:38:12 -0400260
261 gossip_debug(GOSSIP_FILE_DEBUG,
262 "%s-BEGIN(%pU): count(%d) after estimate_max_iovecs.\n",
263 __func__,
264 handle,
265 (int)count);
266
Yi Liu8bb8aef2015-11-24 15:12:14 -0500267 if (type == ORANGEFS_IO_WRITE) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400268 gossip_debug(GOSSIP_FILE_DEBUG,
269 "%s(%pU): proceeding with offset : %llu, "
270 "size %d\n",
271 __func__,
272 handle,
273 llu(*offset),
274 (int)count);
275 }
276
277 if (count == 0) {
278 ret = 0;
279 goto out;
280 }
281
Al Viro0071ed12015-10-08 18:22:08 -0400282 while (iov_iter_count(iter)) {
283 size_t each_count = iov_iter_count(iter);
Mike Marshall5db11c22015-07-17 10:38:12 -0400284 size_t amt_complete;
285
286 /* how much to transfer in this loop iteration */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500287 if (each_count > orangefs_bufmap_size_query())
288 each_count = orangefs_bufmap_size_query();
Mike Marshall5db11c22015-07-17 10:38:12 -0400289
290 gossip_debug(GOSSIP_FILE_DEBUG,
291 "%s(%pU): size of each_count(%d)\n",
292 __func__,
293 handle,
294 (int)each_count);
295 gossip_debug(GOSSIP_FILE_DEBUG,
296 "%s(%pU): BEFORE wait_for_io: offset is %d\n",
297 __func__,
298 handle,
299 (int)*offset);
300
Al Viro0071ed12015-10-08 18:22:08 -0400301 ret = wait_for_direct_io(type, inode, offset, iter,
Al Viro3c2fcfc2015-10-08 18:00:26 -0400302 each_count, 0);
Mike Marshall5db11c22015-07-17 10:38:12 -0400303 gossip_debug(GOSSIP_FILE_DEBUG,
304 "%s(%pU): return from wait_for_io:%d\n",
305 __func__,
306 handle,
307 (int)ret);
308
309 if (ret < 0)
310 goto out;
311
Mike Marshall5db11c22015-07-17 10:38:12 -0400312 *offset += ret;
313 total_count += ret;
314 amt_complete = ret;
315
316 gossip_debug(GOSSIP_FILE_DEBUG,
317 "%s(%pU): AFTER wait_for_io: offset is %d\n",
318 __func__,
319 handle,
320 (int)*offset);
321
322 /*
323 * if we got a short I/O operations,
324 * fall out and return what we got so far
325 */
326 if (amt_complete < each_count)
327 break;
328 } /*end while */
329
330 if (total_count > 0)
331 ret = total_count;
332out:
Mike Marshall5db11c22015-07-17 10:38:12 -0400333 if (ret > 0) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500334 if (type == ORANGEFS_IO_READ) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400335 file_accessed(file);
336 } else {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500337 SetMtimeFlag(orangefs_inode);
Mike Marshall5db11c22015-07-17 10:38:12 -0400338 inode->i_mtime = CURRENT_TIME;
339 mark_inode_dirty_sync(inode);
340 }
341 }
342
343 gossip_debug(GOSSIP_FILE_DEBUG,
344 "%s(%pU): Value(%d) returned.\n",
345 __func__,
346 handle,
347 (int)ret);
348
349 return ret;
350}
351
352/*
353 * Read data from a specified offset in a file (referenced by inode).
354 * Data may be placed either in a user or kernel buffer.
355 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500356ssize_t orangefs_inode_read(struct inode *inode,
357 struct iov_iter *iter,
358 loff_t *offset,
359 loff_t readahead_size)
Mike Marshall5db11c22015-07-17 10:38:12 -0400360{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500361 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
Al Viro74f68fc2015-10-08 18:31:05 -0400362 size_t count = iov_iter_count(iter);
Mike Marshall5db11c22015-07-17 10:38:12 -0400363 size_t bufmap_size;
Mike Marshall5db11c22015-07-17 10:38:12 -0400364 ssize_t ret = -EINVAL;
365
Yi Liu8bb8aef2015-11-24 15:12:14 -0500366 g_orangefs_stats.reads++;
Mike Marshall5db11c22015-07-17 10:38:12 -0400367
Yi Liu8bb8aef2015-11-24 15:12:14 -0500368 bufmap_size = orangefs_bufmap_size_query();
Mike Marshall5db11c22015-07-17 10:38:12 -0400369 if (count > bufmap_size) {
370 gossip_debug(GOSSIP_FILE_DEBUG,
371 "%s: count is too large (%zd/%zd)!\n",
372 __func__, count, bufmap_size);
373 return -EINVAL;
374 }
375
376 gossip_debug(GOSSIP_FILE_DEBUG,
377 "%s(%pU) %zd@%llu\n",
378 __func__,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500379 &orangefs_inode->refn.khandle,
Mike Marshall5db11c22015-07-17 10:38:12 -0400380 count,
381 llu(*offset));
382
Yi Liu8bb8aef2015-11-24 15:12:14 -0500383 ret = wait_for_direct_io(ORANGEFS_IO_READ, inode, offset, iter,
Mike Marshall4d1c4402015-09-04 10:31:16 -0400384 count, readahead_size);
Mike Marshall5db11c22015-07-17 10:38:12 -0400385 if (ret > 0)
386 *offset += ret;
387
388 gossip_debug(GOSSIP_FILE_DEBUG,
389 "%s(%pU): Value(%zd) returned.\n",
390 __func__,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500391 &orangefs_inode->refn.khandle,
Mike Marshall5db11c22015-07-17 10:38:12 -0400392 ret);
393
394 return ret;
395}
396
Yi Liu8bb8aef2015-11-24 15:12:14 -0500397static ssize_t orangefs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Mike Marshall5db11c22015-07-17 10:38:12 -0400398{
399 struct file *file = iocb->ki_filp;
400 loff_t pos = *(&iocb->ki_pos);
401 ssize_t rc = 0;
Mike Marshall5db11c22015-07-17 10:38:12 -0400402
403 BUG_ON(iocb->private);
404
Yi Liu8bb8aef2015-11-24 15:12:14 -0500405 gossip_debug(GOSSIP_FILE_DEBUG, "orangefs_file_read_iter\n");
Mike Marshall5db11c22015-07-17 10:38:12 -0400406
Yi Liu8bb8aef2015-11-24 15:12:14 -0500407 g_orangefs_stats.reads++;
Mike Marshall5db11c22015-07-17 10:38:12 -0400408
Yi Liu8bb8aef2015-11-24 15:12:14 -0500409 rc = do_readv_writev(ORANGEFS_IO_READ, file, &pos, iter);
Mike Marshall5db11c22015-07-17 10:38:12 -0400410 iocb->ki_pos = pos;
411
412 return rc;
413}
414
Yi Liu8bb8aef2015-11-24 15:12:14 -0500415static ssize_t orangefs_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
Mike Marshall5db11c22015-07-17 10:38:12 -0400416{
417 struct file *file = iocb->ki_filp;
Mike Marshall3f1b6942015-11-13 13:05:11 -0500418 loff_t pos;
Mike Marshall5db11c22015-07-17 10:38:12 -0400419 ssize_t rc;
420
421 BUG_ON(iocb->private);
422
Yi Liu8bb8aef2015-11-24 15:12:14 -0500423 gossip_debug(GOSSIP_FILE_DEBUG, "orangefs_file_write_iter\n");
Mike Marshall5db11c22015-07-17 10:38:12 -0400424
425 mutex_lock(&file->f_mapping->host->i_mutex);
426
427 /* Make sure generic_write_checks sees an up to date inode size. */
428 if (file->f_flags & O_APPEND) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500429 rc = orangefs_inode_getattr(file->f_mapping->host,
Martin Brandenburg99109822016-01-28 10:19:40 -0500430 ORANGEFS_ATTR_SYS_SIZE, 0);
Mike Marshall5db11c22015-07-17 10:38:12 -0400431 if (rc) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500432 gossip_err("%s: orangefs_inode_getattr failed, rc:%zd:.\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400433 __func__, rc);
434 goto out;
435 }
436 }
437
438 if (file->f_pos > i_size_read(file->f_mapping->host))
Yi Liu8bb8aef2015-11-24 15:12:14 -0500439 orangefs_i_size_write(file->f_mapping->host, file->f_pos);
Mike Marshall5db11c22015-07-17 10:38:12 -0400440
441 rc = generic_write_checks(iocb, iter);
442
443 if (rc <= 0) {
444 gossip_err("%s: generic_write_checks failed, rc:%zd:.\n",
445 __func__, rc);
446 goto out;
447 }
448
Mike Marshall3f1b6942015-11-13 13:05:11 -0500449 /*
450 * if we are appending, generic_write_checks would have updated
451 * pos to the end of the file, so we will wait till now to set
452 * pos...
453 */
454 pos = *(&iocb->ki_pos);
455
Yi Liu8bb8aef2015-11-24 15:12:14 -0500456 rc = do_readv_writev(ORANGEFS_IO_WRITE,
Mike Marshall5db11c22015-07-17 10:38:12 -0400457 file,
458 &pos,
Al Viro0071ed12015-10-08 18:22:08 -0400459 iter);
Mike Marshall5db11c22015-07-17 10:38:12 -0400460 if (rc < 0) {
461 gossip_err("%s: do_readv_writev failed, rc:%zd:.\n",
462 __func__, rc);
463 goto out;
464 }
465
466 iocb->ki_pos = pos;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500467 g_orangefs_stats.writes++;
Mike Marshall5db11c22015-07-17 10:38:12 -0400468
469out:
470
471 mutex_unlock(&file->f_mapping->host->i_mutex);
472 return rc;
473}
474
475/*
476 * Perform a miscellaneous operation on a file.
477 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500478static long orangefs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Mike Marshall5db11c22015-07-17 10:38:12 -0400479{
480 int ret = -ENOTTY;
481 __u64 val = 0;
482 unsigned long uval;
483
484 gossip_debug(GOSSIP_FILE_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500485 "orangefs_ioctl: called with cmd %d\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400486 cmd);
487
488 /*
489 * we understand some general ioctls on files, such as the immutable
490 * and append flags
491 */
492 if (cmd == FS_IOC_GETFLAGS) {
493 val = 0;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500494 ret = orangefs_inode_getxattr(file_inode(file),
495 ORANGEFS_XATTR_NAME_DEFAULT_PREFIX,
496 "user.pvfs2.meta_hint",
497 &val, sizeof(val));
Mike Marshall5db11c22015-07-17 10:38:12 -0400498 if (ret < 0 && ret != -ENODATA)
499 return ret;
500 else if (ret == -ENODATA)
501 val = 0;
502 uval = val;
503 gossip_debug(GOSSIP_FILE_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500504 "orangefs_ioctl: FS_IOC_GETFLAGS: %llu\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400505 (unsigned long long)uval);
506 return put_user(uval, (int __user *)arg);
507 } else if (cmd == FS_IOC_SETFLAGS) {
508 ret = 0;
509 if (get_user(uval, (int __user *)arg))
510 return -EFAULT;
511 /*
Yi Liu8bb8aef2015-11-24 15:12:14 -0500512 * ORANGEFS_MIRROR_FL is set internally when the mirroring mode
Mike Marshall5db11c22015-07-17 10:38:12 -0400513 * is turned on for a file. The user is not allowed to turn
514 * on this bit, but the bit is present if the user first gets
515 * the flags and then updates the flags with some new
516 * settings. So, we ignore it in the following edit. bligon.
517 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500518 if ((uval & ~ORANGEFS_MIRROR_FL) &
Mike Marshall5db11c22015-07-17 10:38:12 -0400519 (~(FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NOATIME_FL))) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500520 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 -0400521 return -EINVAL;
522 }
523 val = uval;
524 gossip_debug(GOSSIP_FILE_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500525 "orangefs_ioctl: FS_IOC_SETFLAGS: %llu\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400526 (unsigned long long)val);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500527 ret = orangefs_inode_setxattr(file_inode(file),
528 ORANGEFS_XATTR_NAME_DEFAULT_PREFIX,
529 "user.pvfs2.meta_hint",
530 &val, sizeof(val), 0);
Mike Marshall5db11c22015-07-17 10:38:12 -0400531 }
532
533 return ret;
534}
535
536/*
537 * Memory map a region of a file.
538 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500539static int orangefs_file_mmap(struct file *file, struct vm_area_struct *vma)
Mike Marshall5db11c22015-07-17 10:38:12 -0400540{
541 gossip_debug(GOSSIP_FILE_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500542 "orangefs_file_mmap: called on %s\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400543 (file ?
544 (char *)file->f_path.dentry->d_name.name :
545 (char *)"Unknown"));
546
547 /* set the sequential readahead hint */
548 vma->vm_flags |= VM_SEQ_READ;
549 vma->vm_flags &= ~VM_RAND_READ;
Martin Brandenburg35390802015-09-30 13:11:54 -0400550
551 /* Use readonly mmap since we cannot support writable maps. */
552 return generic_file_readonly_mmap(file, vma);
Mike Marshall5db11c22015-07-17 10:38:12 -0400553}
554
555#define mapping_nrpages(idata) ((idata)->nrpages)
556
557/*
558 * Called to notify the module that there are no more references to
559 * this file (i.e. no processes have it open).
560 *
561 * \note Not called when each file is closed.
562 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500563static int orangefs_file_release(struct inode *inode, struct file *file)
Mike Marshall5db11c22015-07-17 10:38:12 -0400564{
565 gossip_debug(GOSSIP_FILE_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500566 "orangefs_file_release: called on %s\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400567 file->f_path.dentry->d_name.name);
568
Yi Liu8bb8aef2015-11-24 15:12:14 -0500569 orangefs_flush_inode(inode);
Mike Marshall5db11c22015-07-17 10:38:12 -0400570
571 /*
Mike Marshall54804942015-10-05 13:44:24 -0400572 * remove all associated inode pages from the page cache and mmap
573 * readahead cache (if any); this forces an expensive refresh of
574 * data for the next caller of mmap (or 'get_block' accesses)
Mike Marshall5db11c22015-07-17 10:38:12 -0400575 */
576 if (file->f_path.dentry->d_inode &&
577 file->f_path.dentry->d_inode->i_mapping &&
578 mapping_nrpages(&file->f_path.dentry->d_inode->i_data))
579 truncate_inode_pages(file->f_path.dentry->d_inode->i_mapping,
580 0);
581 return 0;
582}
583
584/*
585 * Push all data for a specific file onto permanent storage.
586 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500587static int orangefs_fsync(struct file *file,
Mike Marshall84d02152015-07-28 13:27:51 -0400588 loff_t start,
589 loff_t end,
590 int datasync)
Mike Marshall5db11c22015-07-17 10:38:12 -0400591{
592 int ret = -EINVAL;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500593 struct orangefs_inode_s *orangefs_inode =
594 ORANGEFS_I(file->f_path.dentry->d_inode);
595 struct orangefs_kernel_op_s *new_op = NULL;
Mike Marshall5db11c22015-07-17 10:38:12 -0400596
597 /* required call */
598 filemap_write_and_wait_range(file->f_mapping, start, end);
599
Yi Liu8bb8aef2015-11-24 15:12:14 -0500600 new_op = op_alloc(ORANGEFS_VFS_OP_FSYNC);
Mike Marshall5db11c22015-07-17 10:38:12 -0400601 if (!new_op)
602 return -ENOMEM;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500603 new_op->upcall.req.fsync.refn = orangefs_inode->refn;
Mike Marshall5db11c22015-07-17 10:38:12 -0400604
605 ret = service_operation(new_op,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500606 "orangefs_fsync",
Mike Marshall5db11c22015-07-17 10:38:12 -0400607 get_interruptible_flag(file->f_path.dentry->d_inode));
608
609 gossip_debug(GOSSIP_FILE_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500610 "orangefs_fsync got return value of %d\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400611 ret);
612
613 op_release(new_op);
614
Yi Liu8bb8aef2015-11-24 15:12:14 -0500615 orangefs_flush_inode(file->f_path.dentry->d_inode);
Mike Marshall5db11c22015-07-17 10:38:12 -0400616 return ret;
617}
618
619/*
620 * Change the file pointer position for an instance of an open file.
621 *
622 * \note If .llseek is overriden, we must acquire lock as described in
623 * Documentation/filesystems/Locking.
624 *
625 * Future upgrade could support SEEK_DATA and SEEK_HOLE but would
626 * require much changes to the FS
627 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500628static loff_t orangefs_file_llseek(struct file *file, loff_t offset, int origin)
Mike Marshall5db11c22015-07-17 10:38:12 -0400629{
630 int ret = -EINVAL;
631 struct inode *inode = file->f_path.dentry->d_inode;
632
633 if (!inode) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500634 gossip_err("orangefs_file_llseek: invalid inode (NULL)\n");
Mike Marshall5db11c22015-07-17 10:38:12 -0400635 return ret;
636 }
637
Yi Liu8bb8aef2015-11-24 15:12:14 -0500638 if (origin == ORANGEFS_SEEK_END) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400639 /*
640 * revalidate the inode's file size.
641 * NOTE: We are only interested in file size here,
642 * so we set mask accordingly.
643 */
Martin Brandenburg99109822016-01-28 10:19:40 -0500644 ret = orangefs_inode_getattr(inode, ORANGEFS_ATTR_SYS_SIZE, 0);
Mike Marshall5db11c22015-07-17 10:38:12 -0400645 if (ret) {
646 gossip_debug(GOSSIP_FILE_DEBUG,
647 "%s:%s:%d calling make bad inode\n",
648 __FILE__,
649 __func__,
650 __LINE__);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500651 orangefs_make_bad_inode(inode);
Mike Marshall5db11c22015-07-17 10:38:12 -0400652 return ret;
653 }
654 }
655
656 gossip_debug(GOSSIP_FILE_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500657 "orangefs_file_llseek: offset is %ld | origin is %d"
Mike Marshall54804942015-10-05 13:44:24 -0400658 " | inode size is %lu\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400659 (long)offset,
660 origin,
661 (unsigned long)file->f_path.dentry->d_inode->i_size);
662
663 return generic_file_llseek(file, offset, origin);
664}
665
666/*
667 * Support local locks (locks that only this kernel knows about)
668 * if Orangefs was mounted -o local_lock.
669 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500670static int orangefs_lock(struct file *filp, int cmd, struct file_lock *fl)
Mike Marshall5db11c22015-07-17 10:38:12 -0400671{
Mike Marshallf957ae22015-09-24 12:53:05 -0400672 int rc = -EINVAL;
Mike Marshall5db11c22015-07-17 10:38:12 -0400673
Yi Liu8bb8aef2015-11-24 15:12:14 -0500674 if (ORANGEFS_SB(filp->f_inode->i_sb)->flags & ORANGEFS_OPT_LOCAL_LOCK) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400675 if (cmd == F_GETLK) {
676 rc = 0;
677 posix_test_lock(filp, fl);
678 } else {
679 rc = posix_lock_file(filp, fl, NULL);
680 }
681 }
682
683 return rc;
684}
685
Yi Liu8bb8aef2015-11-24 15:12:14 -0500686/** ORANGEFS implementation of VFS file operations */
687const struct file_operations orangefs_file_operations = {
688 .llseek = orangefs_file_llseek,
689 .read_iter = orangefs_file_read_iter,
690 .write_iter = orangefs_file_write_iter,
691 .lock = orangefs_lock,
692 .unlocked_ioctl = orangefs_ioctl,
693 .mmap = orangefs_file_mmap,
Mike Marshall5db11c22015-07-17 10:38:12 -0400694 .open = generic_file_open,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500695 .release = orangefs_file_release,
696 .fsync = orangefs_fsync,
Mike Marshall5db11c22015-07-17 10:38:12 -0400697};