]>
Commit | Line | Data |
---|---|---|
5db11c21 MM |
1 | /* |
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" | |
575e9461 MM |
12 | #include "orangefs-kernel.h" |
13 | #include "orangefs-bufmap.h" | |
5db11c21 MM |
14 | #include <linux/fs.h> |
15 | #include <linux/pagemap.h> | |
16 | ||
5db11c21 MM |
17 | /* |
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 | */ | |
8bb8aefd | 24 | static int precopy_buffers(struct orangefs_bufmap *bufmap, |
5db11c21 | 25 | int buffer_index, |
a5c126a5 | 26 | struct iov_iter *iter, |
4d1c4404 | 27 | size_t total_size) |
5db11c21 MM |
28 | { |
29 | int ret = 0; | |
5db11c21 MM |
30 | /* |
31 | * copy data from application/kernel by pulling it out | |
32 | * of the iovec. | |
33 | */ | |
4d1c4404 MM |
34 | |
35 | ||
36 | if (total_size) { | |
8bb8aefd YL |
37 | ret = orangefs_bufmap_copy_from_iovec(bufmap, |
38 | iter, | |
39 | buffer_index, | |
40 | total_size); | |
4d1c4404 MM |
41 | 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); | |
4d1c4404 MM |
45 | } |
46 | ||
5db11c21 MM |
47 | 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 | */ | |
8bb8aefd | 61 | static int postcopy_buffers(struct orangefs_bufmap *bufmap, |
5db11c21 | 62 | int buffer_index, |
5f0e3c95 | 63 | struct iov_iter *iter, |
4d1c4404 | 64 | size_t total_size) |
5db11c21 MM |
65 | { |
66 | int ret = 0; | |
5db11c21 MM |
67 | /* |
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) { | |
8bb8aefd YL |
73 | ret = orangefs_bufmap_copy_to_iovec(bufmap, |
74 | iter, | |
75 | buffer_index, | |
76 | total_size); | |
5db11c21 | 77 | if (ret < 0) |
4d1c4404 | 78 | gossip_err("%s: Failed to copy-out buffers. Please make sure that the pvfs2-client is running (%ld)\n", |
5db11c21 MM |
79 | __func__, |
80 | (long)ret); | |
81 | } | |
82 | return ret; | |
83 | } | |
84 | ||
85 | /* | |
86 | * Post and wait for the I/O upcall to finish | |
87 | */ | |
8bb8aefd | 88 | static ssize_t wait_for_direct_io(enum ORANGEFS_io_type type, struct inode *inode, |
3c2fcfcb | 89 | loff_t *offset, struct iov_iter *iter, |
4d1c4404 | 90 | size_t total_size, loff_t readahead_size) |
5db11c21 | 91 | { |
8bb8aefd YL |
92 | 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; | |
7b9761af | 96 | struct iov_iter saved = *iter; |
5db11c21 MM |
97 | int buffer_index = -1; |
98 | ssize_t ret; | |
99 | ||
8bb8aefd | 100 | new_op = op_alloc(ORANGEFS_VFS_OP_FILE_IO); |
ed42fe05 AV |
101 | if (!new_op) |
102 | return -ENOMEM; | |
103 | ||
5db11c21 | 104 | /* synchronous I/O */ |
5db11c21 MM |
105 | new_op->upcall.req.io.readahead_size = readahead_size; |
106 | new_op->upcall.req.io.io_type = type; | |
8bb8aefd | 107 | new_op->upcall.req.io.refn = orangefs_inode->refn; |
5db11c21 MM |
108 | |
109 | populate_shared_memory: | |
110 | /* get a shared buffer index */ | |
8bb8aefd | 111 | ret = orangefs_bufmap_get(&bufmap, &buffer_index); |
5db11c21 MM |
112 | if (ret < 0) { |
113 | gossip_debug(GOSSIP_FILE_DEBUG, | |
8bb8aefd | 114 | "%s: orangefs_bufmap_get failure (%ld)\n", |
5db11c21 MM |
115 | __func__, (long)ret); |
116 | goto out; | |
117 | } | |
118 | gossip_debug(GOSSIP_FILE_DEBUG, | |
119 | "%s(%pU): GET op %p -> buffer_index %d\n", | |
120 | __func__, | |
121 | handle, | |
122 | new_op, | |
123 | buffer_index); | |
124 | ||
125 | new_op->uses_shared_memory = 1; | |
126 | new_op->upcall.req.io.buf_index = buffer_index; | |
127 | new_op->upcall.req.io.count = total_size; | |
128 | new_op->upcall.req.io.offset = *offset; | |
129 | ||
130 | gossip_debug(GOSSIP_FILE_DEBUG, | |
3c2fcfcb | 131 | "%s(%pU): offset: %llu total_size: %zd\n", |
5db11c21 MM |
132 | __func__, |
133 | handle, | |
5db11c21 MM |
134 | llu(*offset), |
135 | total_size); | |
136 | /* | |
137 | * Stage 1: copy the buffers into client-core's address space | |
138 | * precopy_buffers only pertains to writes. | |
139 | */ | |
8bb8aefd | 140 | if (type == ORANGEFS_IO_WRITE) { |
5db11c21 MM |
141 | ret = precopy_buffers(bufmap, |
142 | buffer_index, | |
3c2fcfcb | 143 | iter, |
4d1c4404 | 144 | total_size); |
5db11c21 MM |
145 | if (ret < 0) |
146 | goto out; | |
147 | } | |
148 | ||
149 | gossip_debug(GOSSIP_FILE_DEBUG, | |
150 | "%s(%pU): Calling post_io_request with tag (%llu)\n", | |
151 | __func__, | |
152 | handle, | |
153 | llu(new_op->tag)); | |
154 | ||
155 | /* Stage 2: Service the I/O operation */ | |
156 | ret = service_operation(new_op, | |
8bb8aefd | 157 | type == ORANGEFS_IO_WRITE ? |
5db11c21 MM |
158 | "file_write" : |
159 | "file_read", | |
160 | get_interruptible_flag(inode)); | |
161 | ||
162 | /* | |
163 | * If service_operation() returns -EAGAIN #and# the operation was | |
8bb8aefd | 164 | * purged from orangefs_request_list or htable_ops_in_progress, then |
5db11c21 MM |
165 | * we know that the client was restarted, causing the shared memory |
166 | * area to be wiped clean. To restart a write operation in this | |
167 | * case, we must re-copy the data from the user's iovec to a NEW | |
168 | * shared memory location. To restart a read operation, we must get | |
169 | * a new shared memory location. | |
170 | */ | |
171 | if (ret == -EAGAIN && op_state_purged(new_op)) { | |
1357d06d | 172 | orangefs_bufmap_put(buffer_index); |
e17be9fd | 173 | buffer_index = -1; |
7b9761af AV |
174 | if (type == ORANGEFS_IO_WRITE) |
175 | *iter = saved; | |
5db11c21 MM |
176 | gossip_debug(GOSSIP_FILE_DEBUG, |
177 | "%s:going to repopulate_shared_memory.\n", | |
178 | __func__); | |
179 | goto populate_shared_memory; | |
180 | } | |
181 | ||
182 | if (ret < 0) { | |
162ada77 MM |
183 | if (ret == -EINTR) { |
184 | /* | |
185 | * We can't return EINTR if any data was written, | |
186 | * it's not POSIX. It is minimally acceptable | |
187 | * to give a partial write, the way NFS does. | |
188 | * | |
189 | * It would be optimal to return all or nothing, | |
190 | * but if a userspace write is bigger than | |
191 | * an IO buffer, and the interrupt occurs | |
192 | * between buffer writes, that would not be | |
193 | * possible. | |
194 | */ | |
195 | switch (new_op->op_state - OP_VFS_STATE_GIVEN_UP) { | |
196 | /* | |
197 | * If the op was waiting when the interrupt | |
198 | * occurred, then the client-core did not | |
199 | * trigger the write. | |
200 | */ | |
201 | case OP_VFS_STATE_WAITING: | |
202 | if (*offset == 0) | |
203 | ret = -EINTR; | |
204 | else | |
205 | ret = 0; | |
206 | break; | |
207 | /* | |
208 | * If the op was in progress when the interrupt | |
209 | * occurred, then the client-core was able to | |
210 | * trigger the write. | |
211 | */ | |
212 | case OP_VFS_STATE_INPROGR: | |
213 | ret = total_size; | |
214 | break; | |
215 | default: | |
216 | gossip_err("%s: unexpected op state :%d:.\n", | |
217 | __func__, | |
218 | new_op->op_state); | |
219 | ret = 0; | |
220 | break; | |
221 | } | |
5db11c21 | 222 | gossip_debug(GOSSIP_FILE_DEBUG, |
162ada77 MM |
223 | "%s: got EINTR, state:%d: %p\n", |
224 | __func__, | |
225 | new_op->op_state, | |
226 | new_op); | |
227 | } else { | |
5db11c21 MM |
228 | gossip_err("%s: error in %s handle %pU, returning %zd\n", |
229 | __func__, | |
8bb8aefd | 230 | type == ORANGEFS_IO_READ ? |
5db11c21 MM |
231 | "read from" : "write to", |
232 | handle, ret); | |
162ada77 | 233 | } |
78699e29 AV |
234 | if (orangefs_cancel_op_in_progress(new_op)) |
235 | return ret; | |
236 | ||
897c5df6 | 237 | goto out; |
5db11c21 MM |
238 | } |
239 | ||
240 | /* | |
241 | * Stage 3: Post copy buffers from client-core's address space | |
242 | * postcopy_buffers only pertains to reads. | |
243 | */ | |
8bb8aefd | 244 | if (type == ORANGEFS_IO_READ) { |
5db11c21 MM |
245 | ret = postcopy_buffers(bufmap, |
246 | buffer_index, | |
3c2fcfcb | 247 | iter, |
4d1c4404 | 248 | new_op->downcall.resp.io.amt_complete); |
c0eae8cd | 249 | if (ret < 0) |
897c5df6 | 250 | goto out; |
5db11c21 MM |
251 | } |
252 | gossip_debug(GOSSIP_FILE_DEBUG, | |
9d9e7ba9 | 253 | "%s(%pU): Amount %s, returned by the sys-io call:%d\n", |
5db11c21 MM |
254 | __func__, |
255 | handle, | |
9d9e7ba9 | 256 | type == ORANGEFS_IO_READ ? "read" : "written", |
5db11c21 MM |
257 | (int)new_op->downcall.resp.io.amt_complete); |
258 | ||
259 | ret = new_op->downcall.resp.io.amt_complete; | |
260 | ||
5db11c21 MM |
261 | out: |
262 | if (buffer_index >= 0) { | |
1357d06d | 263 | orangefs_bufmap_put(buffer_index); |
5db11c21 MM |
264 | gossip_debug(GOSSIP_FILE_DEBUG, |
265 | "%s(%pU): PUT buffer_index %d\n", | |
266 | __func__, handle, buffer_index); | |
267 | buffer_index = -1; | |
268 | } | |
ed42fe05 | 269 | op_release(new_op); |
5db11c21 MM |
270 | return ret; |
271 | } | |
272 | ||
5db11c21 MM |
273 | /* |
274 | * Common entry point for read/write/readv/writev | |
275 | * This function will dispatch it to either the direct I/O | |
276 | * or buffered I/O path depending on the mount options and/or | |
277 | * augmented/extended metadata attached to the file. | |
278 | * Note: File extended attributes override any mount options. | |
279 | */ | |
8bb8aefd | 280 | static ssize_t do_readv_writev(enum ORANGEFS_io_type type, struct file *file, |
0071ed1e | 281 | loff_t *offset, struct iov_iter *iter) |
5db11c21 MM |
282 | { |
283 | struct inode *inode = file->f_mapping->host; | |
8bb8aefd YL |
284 | struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode); |
285 | struct orangefs_khandle *handle = &orangefs_inode->refn.khandle; | |
0071ed1e | 286 | size_t count = iov_iter_count(iter); |
dc4067f6 AV |
287 | ssize_t total_count = 0; |
288 | ssize_t ret = -EINVAL; | |
5db11c21 MM |
289 | |
290 | gossip_debug(GOSSIP_FILE_DEBUG, | |
291 | "%s-BEGIN(%pU): count(%d) after estimate_max_iovecs.\n", | |
292 | __func__, | |
293 | handle, | |
294 | (int)count); | |
295 | ||
8bb8aefd | 296 | if (type == ORANGEFS_IO_WRITE) { |
5db11c21 MM |
297 | gossip_debug(GOSSIP_FILE_DEBUG, |
298 | "%s(%pU): proceeding with offset : %llu, " | |
299 | "size %d\n", | |
300 | __func__, | |
301 | handle, | |
302 | llu(*offset), | |
303 | (int)count); | |
304 | } | |
305 | ||
306 | if (count == 0) { | |
307 | ret = 0; | |
308 | goto out; | |
309 | } | |
310 | ||
0071ed1e AV |
311 | while (iov_iter_count(iter)) { |
312 | size_t each_count = iov_iter_count(iter); | |
5db11c21 MM |
313 | size_t amt_complete; |
314 | ||
315 | /* how much to transfer in this loop iteration */ | |
8bb8aefd YL |
316 | if (each_count > orangefs_bufmap_size_query()) |
317 | each_count = orangefs_bufmap_size_query(); | |
5db11c21 MM |
318 | |
319 | gossip_debug(GOSSIP_FILE_DEBUG, | |
320 | "%s(%pU): size of each_count(%d)\n", | |
321 | __func__, | |
322 | handle, | |
323 | (int)each_count); | |
324 | gossip_debug(GOSSIP_FILE_DEBUG, | |
325 | "%s(%pU): BEFORE wait_for_io: offset is %d\n", | |
326 | __func__, | |
327 | handle, | |
328 | (int)*offset); | |
329 | ||
0071ed1e | 330 | ret = wait_for_direct_io(type, inode, offset, iter, |
3c2fcfcb | 331 | each_count, 0); |
5db11c21 MM |
332 | gossip_debug(GOSSIP_FILE_DEBUG, |
333 | "%s(%pU): return from wait_for_io:%d\n", | |
334 | __func__, | |
335 | handle, | |
336 | (int)ret); | |
337 | ||
338 | if (ret < 0) | |
339 | goto out; | |
340 | ||
5db11c21 MM |
341 | *offset += ret; |
342 | total_count += ret; | |
343 | amt_complete = ret; | |
344 | ||
345 | gossip_debug(GOSSIP_FILE_DEBUG, | |
346 | "%s(%pU): AFTER wait_for_io: offset is %d\n", | |
347 | __func__, | |
348 | handle, | |
349 | (int)*offset); | |
350 | ||
351 | /* | |
352 | * if we got a short I/O operations, | |
353 | * fall out and return what we got so far | |
354 | */ | |
355 | if (amt_complete < each_count) | |
356 | break; | |
357 | } /*end while */ | |
358 | ||
359 | if (total_count > 0) | |
360 | ret = total_count; | |
361 | out: | |
5db11c21 | 362 | if (ret > 0) { |
8bb8aefd | 363 | if (type == ORANGEFS_IO_READ) { |
5db11c21 MM |
364 | file_accessed(file); |
365 | } else { | |
8bb8aefd | 366 | SetMtimeFlag(orangefs_inode); |
5db11c21 MM |
367 | inode->i_mtime = CURRENT_TIME; |
368 | mark_inode_dirty_sync(inode); | |
369 | } | |
370 | } | |
371 | ||
372 | gossip_debug(GOSSIP_FILE_DEBUG, | |
373 | "%s(%pU): Value(%d) returned.\n", | |
374 | __func__, | |
375 | handle, | |
376 | (int)ret); | |
377 | ||
378 | return ret; | |
379 | } | |
380 | ||
381 | /* | |
382 | * Read data from a specified offset in a file (referenced by inode). | |
383 | * Data may be placed either in a user or kernel buffer. | |
384 | */ | |
8bb8aefd YL |
385 | ssize_t orangefs_inode_read(struct inode *inode, |
386 | struct iov_iter *iter, | |
387 | loff_t *offset, | |
388 | loff_t readahead_size) | |
5db11c21 | 389 | { |
8bb8aefd | 390 | struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode); |
74f68fce | 391 | size_t count = iov_iter_count(iter); |
5db11c21 | 392 | size_t bufmap_size; |
5db11c21 MM |
393 | ssize_t ret = -EINVAL; |
394 | ||
8bb8aefd | 395 | g_orangefs_stats.reads++; |
5db11c21 | 396 | |
8bb8aefd | 397 | bufmap_size = orangefs_bufmap_size_query(); |
5db11c21 MM |
398 | if (count > bufmap_size) { |
399 | gossip_debug(GOSSIP_FILE_DEBUG, | |
400 | "%s: count is too large (%zd/%zd)!\n", | |
401 | __func__, count, bufmap_size); | |
402 | return -EINVAL; | |
403 | } | |
404 | ||
405 | gossip_debug(GOSSIP_FILE_DEBUG, | |
406 | "%s(%pU) %zd@%llu\n", | |
407 | __func__, | |
8bb8aefd | 408 | &orangefs_inode->refn.khandle, |
5db11c21 MM |
409 | count, |
410 | llu(*offset)); | |
411 | ||
8bb8aefd | 412 | ret = wait_for_direct_io(ORANGEFS_IO_READ, inode, offset, iter, |
4d1c4404 | 413 | count, readahead_size); |
5db11c21 MM |
414 | if (ret > 0) |
415 | *offset += ret; | |
416 | ||
417 | gossip_debug(GOSSIP_FILE_DEBUG, | |
418 | "%s(%pU): Value(%zd) returned.\n", | |
419 | __func__, | |
8bb8aefd | 420 | &orangefs_inode->refn.khandle, |
5db11c21 MM |
421 | ret); |
422 | ||
423 | return ret; | |
424 | } | |
425 | ||
8bb8aefd | 426 | static ssize_t orangefs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) |
5db11c21 MM |
427 | { |
428 | struct file *file = iocb->ki_filp; | |
429 | loff_t pos = *(&iocb->ki_pos); | |
430 | ssize_t rc = 0; | |
5db11c21 MM |
431 | |
432 | BUG_ON(iocb->private); | |
433 | ||
8bb8aefd | 434 | gossip_debug(GOSSIP_FILE_DEBUG, "orangefs_file_read_iter\n"); |
5db11c21 | 435 | |
8bb8aefd | 436 | g_orangefs_stats.reads++; |
5db11c21 | 437 | |
8bb8aefd | 438 | rc = do_readv_writev(ORANGEFS_IO_READ, file, &pos, iter); |
5db11c21 MM |
439 | iocb->ki_pos = pos; |
440 | ||
441 | return rc; | |
442 | } | |
443 | ||
8bb8aefd | 444 | static ssize_t orangefs_file_write_iter(struct kiocb *iocb, struct iov_iter *iter) |
5db11c21 MM |
445 | { |
446 | struct file *file = iocb->ki_filp; | |
3f1b6947 | 447 | loff_t pos; |
5db11c21 MM |
448 | ssize_t rc; |
449 | ||
450 | BUG_ON(iocb->private); | |
451 | ||
8bb8aefd | 452 | gossip_debug(GOSSIP_FILE_DEBUG, "orangefs_file_write_iter\n"); |
5db11c21 MM |
453 | |
454 | mutex_lock(&file->f_mapping->host->i_mutex); | |
455 | ||
456 | /* Make sure generic_write_checks sees an up to date inode size. */ | |
457 | if (file->f_flags & O_APPEND) { | |
3c9cf98d | 458 | rc = orangefs_inode_old_getattr(file->f_mapping->host, |
99109822 | 459 | ORANGEFS_ATTR_SYS_SIZE, 0); |
5db11c21 | 460 | if (rc) { |
3c9cf98d | 461 | gossip_err("%s: orangefs_inode_old_getattr failed, rc:%zd:.\n", |
5db11c21 MM |
462 | __func__, rc); |
463 | goto out; | |
464 | } | |
465 | } | |
466 | ||
467 | if (file->f_pos > i_size_read(file->f_mapping->host)) | |
8bb8aefd | 468 | orangefs_i_size_write(file->f_mapping->host, file->f_pos); |
5db11c21 MM |
469 | |
470 | rc = generic_write_checks(iocb, iter); | |
471 | ||
472 | if (rc <= 0) { | |
473 | gossip_err("%s: generic_write_checks failed, rc:%zd:.\n", | |
474 | __func__, rc); | |
475 | goto out; | |
476 | } | |
477 | ||
3f1b6947 MM |
478 | /* |
479 | * if we are appending, generic_write_checks would have updated | |
480 | * pos to the end of the file, so we will wait till now to set | |
481 | * pos... | |
482 | */ | |
483 | pos = *(&iocb->ki_pos); | |
484 | ||
8bb8aefd | 485 | rc = do_readv_writev(ORANGEFS_IO_WRITE, |
5db11c21 MM |
486 | file, |
487 | &pos, | |
0071ed1e | 488 | iter); |
5db11c21 MM |
489 | if (rc < 0) { |
490 | gossip_err("%s: do_readv_writev failed, rc:%zd:.\n", | |
491 | __func__, rc); | |
492 | goto out; | |
493 | } | |
494 | ||
495 | iocb->ki_pos = pos; | |
8bb8aefd | 496 | g_orangefs_stats.writes++; |
5db11c21 MM |
497 | |
498 | out: | |
499 | ||
500 | mutex_unlock(&file->f_mapping->host->i_mutex); | |
501 | return rc; | |
502 | } | |
503 | ||
504 | /* | |
505 | * Perform a miscellaneous operation on a file. | |
506 | */ | |
8bb8aefd | 507 | static long orangefs_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
5db11c21 MM |
508 | { |
509 | int ret = -ENOTTY; | |
510 | __u64 val = 0; | |
511 | unsigned long uval; | |
512 | ||
513 | gossip_debug(GOSSIP_FILE_DEBUG, | |
8bb8aefd | 514 | "orangefs_ioctl: called with cmd %d\n", |
5db11c21 MM |
515 | cmd); |
516 | ||
517 | /* | |
518 | * we understand some general ioctls on files, such as the immutable | |
519 | * and append flags | |
520 | */ | |
521 | if (cmd == FS_IOC_GETFLAGS) { | |
522 | val = 0; | |
8bb8aefd YL |
523 | ret = orangefs_inode_getxattr(file_inode(file), |
524 | ORANGEFS_XATTR_NAME_DEFAULT_PREFIX, | |
525 | "user.pvfs2.meta_hint", | |
526 | &val, sizeof(val)); | |
5db11c21 MM |
527 | if (ret < 0 && ret != -ENODATA) |
528 | return ret; | |
529 | else if (ret == -ENODATA) | |
530 | val = 0; | |
531 | uval = val; | |
532 | gossip_debug(GOSSIP_FILE_DEBUG, | |
8bb8aefd | 533 | "orangefs_ioctl: FS_IOC_GETFLAGS: %llu\n", |
5db11c21 MM |
534 | (unsigned long long)uval); |
535 | return put_user(uval, (int __user *)arg); | |
536 | } else if (cmd == FS_IOC_SETFLAGS) { | |
537 | ret = 0; | |
538 | if (get_user(uval, (int __user *)arg)) | |
539 | return -EFAULT; | |
540 | /* | |
8bb8aefd | 541 | * ORANGEFS_MIRROR_FL is set internally when the mirroring mode |
5db11c21 MM |
542 | * is turned on for a file. The user is not allowed to turn |
543 | * on this bit, but the bit is present if the user first gets | |
544 | * the flags and then updates the flags with some new | |
545 | * settings. So, we ignore it in the following edit. bligon. | |
546 | */ | |
8bb8aefd | 547 | if ((uval & ~ORANGEFS_MIRROR_FL) & |
5db11c21 | 548 | (~(FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NOATIME_FL))) { |
8bb8aefd | 549 | gossip_err("orangefs_ioctl: the FS_IOC_SETFLAGS only supports setting one of FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NOATIME_FL\n"); |
5db11c21 MM |
550 | return -EINVAL; |
551 | } | |
552 | val = uval; | |
553 | gossip_debug(GOSSIP_FILE_DEBUG, | |
8bb8aefd | 554 | "orangefs_ioctl: FS_IOC_SETFLAGS: %llu\n", |
5db11c21 | 555 | (unsigned long long)val); |
8bb8aefd YL |
556 | ret = orangefs_inode_setxattr(file_inode(file), |
557 | ORANGEFS_XATTR_NAME_DEFAULT_PREFIX, | |
558 | "user.pvfs2.meta_hint", | |
559 | &val, sizeof(val), 0); | |
5db11c21 MM |
560 | } |
561 | ||
562 | return ret; | |
563 | } | |
564 | ||
565 | /* | |
566 | * Memory map a region of a file. | |
567 | */ | |
8bb8aefd | 568 | static int orangefs_file_mmap(struct file *file, struct vm_area_struct *vma) |
5db11c21 MM |
569 | { |
570 | gossip_debug(GOSSIP_FILE_DEBUG, | |
8bb8aefd | 571 | "orangefs_file_mmap: called on %s\n", |
5db11c21 MM |
572 | (file ? |
573 | (char *)file->f_path.dentry->d_name.name : | |
574 | (char *)"Unknown")); | |
575 | ||
576 | /* set the sequential readahead hint */ | |
577 | vma->vm_flags |= VM_SEQ_READ; | |
578 | vma->vm_flags &= ~VM_RAND_READ; | |
35390803 MB |
579 | |
580 | /* Use readonly mmap since we cannot support writable maps. */ | |
581 | return generic_file_readonly_mmap(file, vma); | |
5db11c21 MM |
582 | } |
583 | ||
584 | #define mapping_nrpages(idata) ((idata)->nrpages) | |
585 | ||
586 | /* | |
587 | * Called to notify the module that there are no more references to | |
588 | * this file (i.e. no processes have it open). | |
589 | * | |
590 | * \note Not called when each file is closed. | |
591 | */ | |
8bb8aefd | 592 | static int orangefs_file_release(struct inode *inode, struct file *file) |
5db11c21 MM |
593 | { |
594 | gossip_debug(GOSSIP_FILE_DEBUG, | |
8bb8aefd | 595 | "orangefs_file_release: called on %s\n", |
5db11c21 MM |
596 | file->f_path.dentry->d_name.name); |
597 | ||
8bb8aefd | 598 | orangefs_flush_inode(inode); |
5db11c21 MM |
599 | |
600 | /* | |
54804949 MM |
601 | * remove all associated inode pages from the page cache and mmap |
602 | * readahead cache (if any); this forces an expensive refresh of | |
603 | * data for the next caller of mmap (or 'get_block' accesses) | |
5db11c21 MM |
604 | */ |
605 | if (file->f_path.dentry->d_inode && | |
606 | file->f_path.dentry->d_inode->i_mapping && | |
607 | mapping_nrpages(&file->f_path.dentry->d_inode->i_data)) | |
608 | truncate_inode_pages(file->f_path.dentry->d_inode->i_mapping, | |
609 | 0); | |
610 | return 0; | |
611 | } | |
612 | ||
613 | /* | |
614 | * Push all data for a specific file onto permanent storage. | |
615 | */ | |
8bb8aefd | 616 | static int orangefs_fsync(struct file *file, |
84d02150 MM |
617 | loff_t start, |
618 | loff_t end, | |
619 | int datasync) | |
5db11c21 MM |
620 | { |
621 | int ret = -EINVAL; | |
8bb8aefd YL |
622 | struct orangefs_inode_s *orangefs_inode = |
623 | ORANGEFS_I(file->f_path.dentry->d_inode); | |
624 | struct orangefs_kernel_op_s *new_op = NULL; | |
5db11c21 MM |
625 | |
626 | /* required call */ | |
627 | filemap_write_and_wait_range(file->f_mapping, start, end); | |
628 | ||
8bb8aefd | 629 | new_op = op_alloc(ORANGEFS_VFS_OP_FSYNC); |
5db11c21 MM |
630 | if (!new_op) |
631 | return -ENOMEM; | |
8bb8aefd | 632 | new_op->upcall.req.fsync.refn = orangefs_inode->refn; |
5db11c21 MM |
633 | |
634 | ret = service_operation(new_op, | |
8bb8aefd | 635 | "orangefs_fsync", |
5db11c21 MM |
636 | get_interruptible_flag(file->f_path.dentry->d_inode)); |
637 | ||
638 | gossip_debug(GOSSIP_FILE_DEBUG, | |
8bb8aefd | 639 | "orangefs_fsync got return value of %d\n", |
5db11c21 MM |
640 | ret); |
641 | ||
642 | op_release(new_op); | |
643 | ||
8bb8aefd | 644 | orangefs_flush_inode(file->f_path.dentry->d_inode); |
5db11c21 MM |
645 | return ret; |
646 | } | |
647 | ||
648 | /* | |
649 | * Change the file pointer position for an instance of an open file. | |
650 | * | |
651 | * \note If .llseek is overriden, we must acquire lock as described in | |
652 | * Documentation/filesystems/Locking. | |
653 | * | |
654 | * Future upgrade could support SEEK_DATA and SEEK_HOLE but would | |
655 | * require much changes to the FS | |
656 | */ | |
8bb8aefd | 657 | static loff_t orangefs_file_llseek(struct file *file, loff_t offset, int origin) |
5db11c21 MM |
658 | { |
659 | int ret = -EINVAL; | |
660 | struct inode *inode = file->f_path.dentry->d_inode; | |
661 | ||
662 | if (!inode) { | |
8bb8aefd | 663 | gossip_err("orangefs_file_llseek: invalid inode (NULL)\n"); |
5db11c21 MM |
664 | return ret; |
665 | } | |
666 | ||
8bb8aefd | 667 | if (origin == ORANGEFS_SEEK_END) { |
5db11c21 MM |
668 | /* |
669 | * revalidate the inode's file size. | |
670 | * NOTE: We are only interested in file size here, | |
671 | * so we set mask accordingly. | |
672 | */ | |
3c9cf98d MB |
673 | ret = orangefs_inode_old_getattr(inode, |
674 | ORANGEFS_ATTR_SYS_SIZE, 0); | |
5db11c21 MM |
675 | if (ret) { |
676 | gossip_debug(GOSSIP_FILE_DEBUG, | |
677 | "%s:%s:%d calling make bad inode\n", | |
678 | __FILE__, | |
679 | __func__, | |
680 | __LINE__); | |
8bb8aefd | 681 | orangefs_make_bad_inode(inode); |
5db11c21 MM |
682 | return ret; |
683 | } | |
684 | } | |
685 | ||
686 | gossip_debug(GOSSIP_FILE_DEBUG, | |
8bb8aefd | 687 | "orangefs_file_llseek: offset is %ld | origin is %d" |
54804949 | 688 | " | inode size is %lu\n", |
5db11c21 MM |
689 | (long)offset, |
690 | origin, | |
691 | (unsigned long)file->f_path.dentry->d_inode->i_size); | |
692 | ||
693 | return generic_file_llseek(file, offset, origin); | |
694 | } | |
695 | ||
696 | /* | |
697 | * Support local locks (locks that only this kernel knows about) | |
698 | * if Orangefs was mounted -o local_lock. | |
699 | */ | |
8bb8aefd | 700 | static int orangefs_lock(struct file *filp, int cmd, struct file_lock *fl) |
5db11c21 | 701 | { |
f957ae2d | 702 | int rc = -EINVAL; |
5db11c21 | 703 | |
8bb8aefd | 704 | if (ORANGEFS_SB(filp->f_inode->i_sb)->flags & ORANGEFS_OPT_LOCAL_LOCK) { |
5db11c21 MM |
705 | if (cmd == F_GETLK) { |
706 | rc = 0; | |
707 | posix_test_lock(filp, fl); | |
708 | } else { | |
709 | rc = posix_lock_file(filp, fl, NULL); | |
710 | } | |
711 | } | |
712 | ||
713 | return rc; | |
714 | } | |
715 | ||
8bb8aefd YL |
716 | /** ORANGEFS implementation of VFS file operations */ |
717 | const struct file_operations orangefs_file_operations = { | |
718 | .llseek = orangefs_file_llseek, | |
719 | .read_iter = orangefs_file_read_iter, | |
720 | .write_iter = orangefs_file_write_iter, | |
721 | .lock = orangefs_lock, | |
722 | .unlocked_ioctl = orangefs_ioctl, | |
723 | .mmap = orangefs_file_mmap, | |
5db11c21 | 724 | .open = generic_file_open, |
8bb8aefd YL |
725 | .release = orangefs_file_release, |
726 | .fsync = orangefs_fsync, | |
5db11c21 | 727 | }; |