file.c 19.4 KB
Newer Older
M
Mike Marshall 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * (C) 2001 Clemson University and The University of Chicago
 *
 * See COPYING in top-level directory.
 */

/*
 *  Linux VFS file operations.
 */

#include "protocol.h"
12 13
#include "orangefs-kernel.h"
#include "orangefs-bufmap.h"
M
Mike Marshall 已提交
14 15 16 17 18 19 20 21 22 23
#include <linux/fs.h>
#include <linux/pagemap.h>

/*
 * Copy to client-core's address space from the buffers specified
 * by the iovec upto total_size bytes.
 * NOTE: the iovector can either contain addresses which
 *       can futher be kernel-space or user-space addresses.
 *       or it can pointers to struct page's
 */
24
static int precopy_buffers(struct orangefs_bufmap *bufmap,
M
Mike Marshall 已提交
25
			   int buffer_index,
26
			   struct iov_iter *iter,
M
Mike Marshall 已提交
27
			   size_t total_size)
M
Mike Marshall 已提交
28 29 30 31 32 33
{
	int ret = 0;
	/*
	 * copy data from application/kernel by pulling it out
	 * of the iovec.
	 */
M
Mike Marshall 已提交
34 35 36


	if (total_size) {
37 38 39 40
		ret = orangefs_bufmap_copy_from_iovec(bufmap,
						      iter,
						      buffer_index,
						      total_size);
M
Mike Marshall 已提交
41 42 43 44 45 46
		if (ret < 0)
		gossip_err("%s: Failed to copy-in buffers. Please make sure that the pvfs2-client is running. %ld\n",
			   __func__,
			   (long)ret);
	}

M
Mike Marshall 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60
	if (ret < 0)
		gossip_err("%s: Failed to copy-in buffers. Please make sure that the pvfs2-client is running. %ld\n",
			__func__,
			(long)ret);
	return ret;
}

/*
 * Copy from client-core's address space to the buffers specified
 * by the iovec upto total_size bytes.
 * NOTE: the iovector can either contain addresses which
 *       can futher be kernel-space or user-space addresses.
 *       or it can pointers to struct page's
 */
61
static int postcopy_buffers(struct orangefs_bufmap *bufmap,
M
Mike Marshall 已提交
62
			    int buffer_index,
63
			    struct iov_iter *iter,
M
Mike Marshall 已提交
64
			    size_t total_size)
M
Mike Marshall 已提交
65 66 67 68 69 70 71 72
{
	int ret = 0;
	/*
	 * copy data to application/kernel by pushing it out to
	 * the iovec. NOTE; target buffers can be addresses or
	 * struct page pointers.
	 */
	if (total_size) {
73 74 75 76
		ret = orangefs_bufmap_copy_to_iovec(bufmap,
						    iter,
						    buffer_index,
						    total_size);
M
Mike Marshall 已提交
77
		if (ret < 0)
M
Mike Marshall 已提交
78
			gossip_err("%s: Failed to copy-out buffers. Please make sure that the pvfs2-client is running (%ld)\n",
M
Mike Marshall 已提交
79 80 81 82 83 84
				__func__,
				(long)ret);
	}
	return ret;
}

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
/*
 * handles two possible error cases, depending on context.
 *
 * by design, our vfs i/o errors need to be handled in one of two ways,
 * depending on where the error occured.
 *
 * if the error happens in the waitqueue code because we either timed
 * out or a signal was raised while waiting, we need to cancel the
 * userspace i/o operation and free the op manually.  this is done to
 * avoid having the device start writing application data to our shared
 * bufmap pages without us expecting it.
 *
 * FIXME: POSSIBLE OPTIMIZATION:
 * However, if we timed out or if we got a signal AND our upcall was never
 * picked off the queue (i.e. we were in OP_VFS_STATE_WAITING), then we don't
 * need to send a cancellation upcall. The way we can handle this is
 * set error_exit to 2 in such cases and 1 whenever cancellation has to be
 * sent and have handle_error
 * take care of this situation as well..
 *
 * if a orangefs sysint level error occured and i/o has been completed,
 * there is no need to cancel the operation, as the user has finished
 * using the bufmap page and so there is no danger in this case.  in
 * this case, we wake up the device normally so that it may free the
 * op, as normal.
 *
 * note the only reason this is a macro is because both read and write
 * cases need the exact same handling code.
 */
#define handle_io_error()					\
do {								\
	if (!op_state_serviced(new_op)) {			\
		orangefs_cancel_op_in_progress(new_op->tag);	\
	} else {						\
		complete(&new_op->done);			\
	}							\
	orangefs_bufmap_put(bufmap, buffer_index);		\
	buffer_index = -1;					\
} while (0)

M
Mike Marshall 已提交
125 126 127
/*
 * Post and wait for the I/O upcall to finish
 */
128
static ssize_t wait_for_direct_io(enum ORANGEFS_io_type type, struct inode *inode,
129
		loff_t *offset, struct iov_iter *iter,
M
Mike Marshall 已提交
130
		size_t total_size, loff_t readahead_size)
M
Mike Marshall 已提交
131
{
132 133 134 135
	struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
	struct orangefs_khandle *handle = &orangefs_inode->refn.khandle;
	struct orangefs_bufmap *bufmap = NULL;
	struct orangefs_kernel_op_s *new_op = NULL;
M
Mike Marshall 已提交
136 137 138
	int buffer_index = -1;
	ssize_t ret;

139
	new_op = op_alloc(ORANGEFS_VFS_OP_FILE_IO);
140 141 142
	if (!new_op)
		return -ENOMEM;

M
Mike Marshall 已提交
143
	/* synchronous I/O */
144
	new_op->upcall.req.io.async_vfs_io = ORANGEFS_VFS_SYNC_IO;
M
Mike Marshall 已提交
145 146
	new_op->upcall.req.io.readahead_size = readahead_size;
	new_op->upcall.req.io.io_type = type;
147
	new_op->upcall.req.io.refn = orangefs_inode->refn;
M
Mike Marshall 已提交
148 149 150

populate_shared_memory:
	/* get a shared buffer index */
151
	ret = orangefs_bufmap_get(&bufmap, &buffer_index);
M
Mike Marshall 已提交
152 153
	if (ret < 0) {
		gossip_debug(GOSSIP_FILE_DEBUG,
154
			     "%s: orangefs_bufmap_get failure (%ld)\n",
M
Mike Marshall 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
			     __func__, (long)ret);
		goto out;
	}
	gossip_debug(GOSSIP_FILE_DEBUG,
		     "%s(%pU): GET op %p -> buffer_index %d\n",
		     __func__,
		     handle,
		     new_op,
		     buffer_index);

	new_op->uses_shared_memory = 1;
	new_op->upcall.req.io.buf_index = buffer_index;
	new_op->upcall.req.io.count = total_size;
	new_op->upcall.req.io.offset = *offset;

	gossip_debug(GOSSIP_FILE_DEBUG,
171
		     "%s(%pU): offset: %llu total_size: %zd\n",
M
Mike Marshall 已提交
172 173 174 175 176 177 178 179
		     __func__,
		     handle,
		     llu(*offset),
		     total_size);
	/*
	 * Stage 1: copy the buffers into client-core's address space
	 * precopy_buffers only pertains to writes.
	 */
180
	if (type == ORANGEFS_IO_WRITE) {
M
Mike Marshall 已提交
181 182
		ret = precopy_buffers(bufmap,
				      buffer_index,
183
				      iter,
M
Mike Marshall 已提交
184
				      total_size);
M
Mike Marshall 已提交
185 186 187 188 189 190 191 192 193 194 195 196
		if (ret < 0)
			goto out;
	}

	gossip_debug(GOSSIP_FILE_DEBUG,
		     "%s(%pU): Calling post_io_request with tag (%llu)\n",
		     __func__,
		     handle,
		     llu(new_op->tag));

	/* Stage 2: Service the I/O operation */
	ret = service_operation(new_op,
197
				type == ORANGEFS_IO_WRITE ?
M
Mike Marshall 已提交
198 199 200 201 202 203
					"file_write" :
					"file_read",
				get_interruptible_flag(inode));

	/*
	 * If service_operation() returns -EAGAIN #and# the operation was
204
	 * purged from orangefs_request_list or htable_ops_in_progress, then
M
Mike Marshall 已提交
205 206 207 208 209 210 211
	 * we know that the client was restarted, causing the shared memory
	 * area to be wiped clean.  To restart a  write operation in this
	 * case, we must re-copy the data from the user's iovec to a NEW
	 * shared memory location. To restart a read operation, we must get
	 * a new shared memory location.
	 */
	if (ret == -EAGAIN && op_state_purged(new_op)) {
212
		orangefs_bufmap_put(bufmap, buffer_index);
213
		buffer_index = -1;
M
Mike Marshall 已提交
214 215 216 217 218 219 220
		gossip_debug(GOSSIP_FILE_DEBUG,
			     "%s:going to repopulate_shared_memory.\n",
			     __func__);
		goto populate_shared_memory;
	}

	if (ret < 0) {
221
		handle_io_error();
M
Mike Marshall 已提交
222
		/*
223 224 225
		 * don't write an error to syslog on signaled operation
		 * termination unless we've got debugging turned on, as
		 * this can happen regularly (i.e. ctrl-c)
M
Mike Marshall 已提交
226 227 228 229 230 231 232 233
		 */
		if (ret == -EINTR)
			gossip_debug(GOSSIP_FILE_DEBUG,
				     "%s: returning error %ld\n", __func__,
				     (long)ret);
		else
			gossip_err("%s: error in %s handle %pU, returning %zd\n",
				__func__,
234
				type == ORANGEFS_IO_READ ?
M
Mike Marshall 已提交
235 236 237 238 239 240 241 242 243
					"read from" : "write to",
				handle, ret);
		goto out;
	}

	/*
	 * Stage 3: Post copy buffers from client-core's address space
	 * postcopy_buffers only pertains to reads.
	 */
244
	if (type == ORANGEFS_IO_READ) {
M
Mike Marshall 已提交
245 246
		ret = postcopy_buffers(bufmap,
				       buffer_index,
247
				       iter,
M
Mike Marshall 已提交
248
				       new_op->downcall.resp.io.amt_complete);
M
Mike Marshall 已提交
249 250 251 252 253
		if (ret < 0) {
			/*
			 * put error codes in downcall so that handle_io_error()
			 * preserves it properly
			 */
254
			WARN_ON(!op_state_serviced(new_op));
M
Mike Marshall 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268
			new_op->downcall.status = ret;
			handle_io_error();
			goto out;
		}
	}
	gossip_debug(GOSSIP_FILE_DEBUG,
	    "%s(%pU): Amount written as returned by the sys-io call:%d\n",
	    __func__,
	    handle,
	    (int)new_op->downcall.resp.io.amt_complete);

	ret = new_op->downcall.resp.io.amt_complete;

	/*
269
	 * tell the device file owner waiting on I/O that this read has
270
	 * completed and it can return now.
M
Mike Marshall 已提交
271
	 */
272
	complete(&new_op->done);
M
Mike Marshall 已提交
273 274 275

out:
	if (buffer_index >= 0) {
276
		orangefs_bufmap_put(bufmap, buffer_index);
M
Mike Marshall 已提交
277 278 279 280 281
		gossip_debug(GOSSIP_FILE_DEBUG,
			     "%s(%pU): PUT buffer_index %d\n",
			     __func__, handle, buffer_index);
		buffer_index = -1;
	}
282
	op_release(new_op);
M
Mike Marshall 已提交
283 284 285 286 287 288 289 290 291 292
	return ret;
}

/*
 * Common entry point for read/write/readv/writev
 * This function will dispatch it to either the direct I/O
 * or buffered I/O path depending on the mount options and/or
 * augmented/extended metadata attached to the file.
 * Note: File extended attributes override any mount options.
 */
293
static ssize_t do_readv_writev(enum ORANGEFS_io_type type, struct file *file,
294
		loff_t *offset, struct iov_iter *iter)
M
Mike Marshall 已提交
295 296
{
	struct inode *inode = file->f_mapping->host;
297 298
	struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
	struct orangefs_khandle *handle = &orangefs_inode->refn.khandle;
299
	size_t count = iov_iter_count(iter);
300 301
	ssize_t total_count = 0;
	ssize_t ret = -EINVAL;
M
Mike Marshall 已提交
302 303 304 305 306 307 308

	gossip_debug(GOSSIP_FILE_DEBUG,
		"%s-BEGIN(%pU): count(%d) after estimate_max_iovecs.\n",
		__func__,
		handle,
		(int)count);

309
	if (type == ORANGEFS_IO_WRITE) {
M
Mike Marshall 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323
		gossip_debug(GOSSIP_FILE_DEBUG,
			     "%s(%pU): proceeding with offset : %llu, "
			     "size %d\n",
			     __func__,
			     handle,
			     llu(*offset),
			     (int)count);
	}

	if (count == 0) {
		ret = 0;
		goto out;
	}

324 325
	while (iov_iter_count(iter)) {
		size_t each_count = iov_iter_count(iter);
M
Mike Marshall 已提交
326 327 328
		size_t amt_complete;

		/* how much to transfer in this loop iteration */
329 330
		if (each_count > orangefs_bufmap_size_query())
			each_count = orangefs_bufmap_size_query();
M
Mike Marshall 已提交
331 332 333 334 335 336 337 338 339 340 341 342

		gossip_debug(GOSSIP_FILE_DEBUG,
			     "%s(%pU): size of each_count(%d)\n",
			     __func__,
			     handle,
			     (int)each_count);
		gossip_debug(GOSSIP_FILE_DEBUG,
			     "%s(%pU): BEFORE wait_for_io: offset is %d\n",
			     __func__,
			     handle,
			     (int)*offset);

343
		ret = wait_for_direct_io(type, inode, offset, iter,
344
				each_count, 0);
M
Mike Marshall 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
		gossip_debug(GOSSIP_FILE_DEBUG,
			     "%s(%pU): return from wait_for_io:%d\n",
			     __func__,
			     handle,
			     (int)ret);

		if (ret < 0)
			goto out;

		*offset += ret;
		total_count += ret;
		amt_complete = ret;

		gossip_debug(GOSSIP_FILE_DEBUG,
			     "%s(%pU): AFTER wait_for_io: offset is %d\n",
			     __func__,
			     handle,
			     (int)*offset);

		/*
		 * if we got a short I/O operations,
		 * fall out and return what we got so far
		 */
		if (amt_complete < each_count)
			break;
	} /*end while */

	if (total_count > 0)
		ret = total_count;
out:
	if (ret > 0) {
376
		if (type == ORANGEFS_IO_READ) {
M
Mike Marshall 已提交
377 378
			file_accessed(file);
		} else {
379
			SetMtimeFlag(orangefs_inode);
M
Mike Marshall 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
			inode->i_mtime = CURRENT_TIME;
			mark_inode_dirty_sync(inode);
		}
	}

	gossip_debug(GOSSIP_FILE_DEBUG,
		     "%s(%pU): Value(%d) returned.\n",
		     __func__,
		     handle,
		     (int)ret);

	return ret;
}

/*
 * Read data from a specified offset in a file (referenced by inode).
 * Data may be placed either in a user or kernel buffer.
 */
398 399 400 401
ssize_t orangefs_inode_read(struct inode *inode,
			    struct iov_iter *iter,
			    loff_t *offset,
			    loff_t readahead_size)
M
Mike Marshall 已提交
402
{
403
	struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
404
	size_t count = iov_iter_count(iter);
M
Mike Marshall 已提交
405 406 407
	size_t bufmap_size;
	ssize_t ret = -EINVAL;

408
	g_orangefs_stats.reads++;
M
Mike Marshall 已提交
409

410
	bufmap_size = orangefs_bufmap_size_query();
M
Mike Marshall 已提交
411 412 413 414 415 416 417 418 419 420
	if (count > bufmap_size) {
		gossip_debug(GOSSIP_FILE_DEBUG,
			     "%s: count is too large (%zd/%zd)!\n",
			     __func__, count, bufmap_size);
		return -EINVAL;
	}

	gossip_debug(GOSSIP_FILE_DEBUG,
		     "%s(%pU) %zd@%llu\n",
		     __func__,
421
		     &orangefs_inode->refn.khandle,
M
Mike Marshall 已提交
422 423 424
		     count,
		     llu(*offset));

425
	ret = wait_for_direct_io(ORANGEFS_IO_READ, inode, offset, iter,
M
Mike Marshall 已提交
426
			count, readahead_size);
M
Mike Marshall 已提交
427 428 429 430 431 432
	if (ret > 0)
		*offset += ret;

	gossip_debug(GOSSIP_FILE_DEBUG,
		     "%s(%pU): Value(%zd) returned.\n",
		     __func__,
433
		     &orangefs_inode->refn.khandle,
M
Mike Marshall 已提交
434 435 436 437 438
		     ret);

	return ret;
}

439
static ssize_t orangefs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
M
Mike Marshall 已提交
440 441 442 443 444 445 446
{
	struct file *file = iocb->ki_filp;
	loff_t pos = *(&iocb->ki_pos);
	ssize_t rc = 0;

	BUG_ON(iocb->private);

447
	gossip_debug(GOSSIP_FILE_DEBUG, "orangefs_file_read_iter\n");
M
Mike Marshall 已提交
448

449
	g_orangefs_stats.reads++;
M
Mike Marshall 已提交
450

451
	rc = do_readv_writev(ORANGEFS_IO_READ, file, &pos, iter);
M
Mike Marshall 已提交
452 453 454 455 456
	iocb->ki_pos = pos;

	return rc;
}

457
static ssize_t orangefs_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
M
Mike Marshall 已提交
458 459
{
	struct file *file = iocb->ki_filp;
460
	loff_t pos;
M
Mike Marshall 已提交
461 462 463 464
	ssize_t rc;

	BUG_ON(iocb->private);

465
	gossip_debug(GOSSIP_FILE_DEBUG, "orangefs_file_write_iter\n");
M
Mike Marshall 已提交
466 467 468 469 470

	mutex_lock(&file->f_mapping->host->i_mutex);

	/* Make sure generic_write_checks sees an up to date inode size. */
	if (file->f_flags & O_APPEND) {
471
		rc = orangefs_inode_getattr(file->f_mapping->host,
M
Martin Brandenburg 已提交
472
					 ORANGEFS_ATTR_SYS_SIZE, 0);
M
Mike Marshall 已提交
473
		if (rc) {
474
			gossip_err("%s: orangefs_inode_getattr failed, rc:%zd:.\n",
M
Mike Marshall 已提交
475 476 477 478 479 480
				   __func__, rc);
			goto out;
		}
	}

	if (file->f_pos > i_size_read(file->f_mapping->host))
481
		orangefs_i_size_write(file->f_mapping->host, file->f_pos);
M
Mike Marshall 已提交
482 483 484 485 486 487 488 489 490

	rc = generic_write_checks(iocb, iter);

	if (rc <= 0) {
		gossip_err("%s: generic_write_checks failed, rc:%zd:.\n",
			   __func__, rc);
		goto out;
	}

491 492 493 494 495 496 497
	/*
	 * if we are appending, generic_write_checks would have updated
	 * pos to the end of the file, so we will wait till now to set
	 * pos...
	 */
	pos = *(&iocb->ki_pos);

498
	rc = do_readv_writev(ORANGEFS_IO_WRITE,
M
Mike Marshall 已提交
499 500
			     file,
			     &pos,
501
			     iter);
M
Mike Marshall 已提交
502 503 504 505 506 507 508
	if (rc < 0) {
		gossip_err("%s: do_readv_writev failed, rc:%zd:.\n",
			   __func__, rc);
		goto out;
	}

	iocb->ki_pos = pos;
509
	g_orangefs_stats.writes++;
M
Mike Marshall 已提交
510 511 512 513 514 515 516 517 518 519

out:

	mutex_unlock(&file->f_mapping->host->i_mutex);
	return rc;
}

/*
 * Perform a miscellaneous operation on a file.
 */
520
static long orangefs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
M
Mike Marshall 已提交
521 522 523 524 525 526
{
	int ret = -ENOTTY;
	__u64 val = 0;
	unsigned long uval;

	gossip_debug(GOSSIP_FILE_DEBUG,
527
		     "orangefs_ioctl: called with cmd %d\n",
M
Mike Marshall 已提交
528 529 530 531 532 533 534 535
		     cmd);

	/*
	 * we understand some general ioctls on files, such as the immutable
	 * and append flags
	 */
	if (cmd == FS_IOC_GETFLAGS) {
		val = 0;
536 537 538 539
		ret = orangefs_inode_getxattr(file_inode(file),
					      ORANGEFS_XATTR_NAME_DEFAULT_PREFIX,
					      "user.pvfs2.meta_hint",
					      &val, sizeof(val));
M
Mike Marshall 已提交
540 541 542 543 544 545
		if (ret < 0 && ret != -ENODATA)
			return ret;
		else if (ret == -ENODATA)
			val = 0;
		uval = val;
		gossip_debug(GOSSIP_FILE_DEBUG,
546
			     "orangefs_ioctl: FS_IOC_GETFLAGS: %llu\n",
M
Mike Marshall 已提交
547 548 549 550 551 552 553
			     (unsigned long long)uval);
		return put_user(uval, (int __user *)arg);
	} else if (cmd == FS_IOC_SETFLAGS) {
		ret = 0;
		if (get_user(uval, (int __user *)arg))
			return -EFAULT;
		/*
554
		 * ORANGEFS_MIRROR_FL is set internally when the mirroring mode
M
Mike Marshall 已提交
555 556 557 558 559
		 * is turned on for a file. The user is not allowed to turn
		 * on this bit, but the bit is present if the user first gets
		 * the flags and then updates the flags with some new
		 * settings. So, we ignore it in the following edit. bligon.
		 */
560
		if ((uval & ~ORANGEFS_MIRROR_FL) &
M
Mike Marshall 已提交
561
		    (~(FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NOATIME_FL))) {
562
			gossip_err("orangefs_ioctl: the FS_IOC_SETFLAGS only supports setting one of FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NOATIME_FL\n");
M
Mike Marshall 已提交
563 564 565 566
			return -EINVAL;
		}
		val = uval;
		gossip_debug(GOSSIP_FILE_DEBUG,
567
			     "orangefs_ioctl: FS_IOC_SETFLAGS: %llu\n",
M
Mike Marshall 已提交
568
			     (unsigned long long)val);
569 570 571 572
		ret = orangefs_inode_setxattr(file_inode(file),
					      ORANGEFS_XATTR_NAME_DEFAULT_PREFIX,
					      "user.pvfs2.meta_hint",
					      &val, sizeof(val), 0);
M
Mike Marshall 已提交
573 574 575 576 577 578 579 580
	}

	return ret;
}

/*
 * Memory map a region of a file.
 */
581
static int orangefs_file_mmap(struct file *file, struct vm_area_struct *vma)
M
Mike Marshall 已提交
582 583
{
	gossip_debug(GOSSIP_FILE_DEBUG,
584
		     "orangefs_file_mmap: called on %s\n",
M
Mike Marshall 已提交
585 586 587 588 589 590 591
		     (file ?
			(char *)file->f_path.dentry->d_name.name :
			(char *)"Unknown"));

	/* set the sequential readahead hint */
	vma->vm_flags |= VM_SEQ_READ;
	vma->vm_flags &= ~VM_RAND_READ;
592 593 594

	/* Use readonly mmap since we cannot support writable maps. */
	return generic_file_readonly_mmap(file, vma);
M
Mike Marshall 已提交
595 596 597 598 599 600 601 602 603 604
}

#define mapping_nrpages(idata) ((idata)->nrpages)

/*
 * Called to notify the module that there are no more references to
 * this file (i.e. no processes have it open).
 *
 * \note Not called when each file is closed.
 */
605
static int orangefs_file_release(struct inode *inode, struct file *file)
M
Mike Marshall 已提交
606 607
{
	gossip_debug(GOSSIP_FILE_DEBUG,
608
		     "orangefs_file_release: called on %s\n",
M
Mike Marshall 已提交
609 610
		     file->f_path.dentry->d_name.name);

611
	orangefs_flush_inode(inode);
M
Mike Marshall 已提交
612 613

	/*
614 615 616
	 * remove all associated inode pages from the page cache and mmap
	 * readahead cache (if any); this forces an expensive refresh of
	 * data for the next caller of mmap (or 'get_block' accesses)
M
Mike Marshall 已提交
617 618 619 620 621 622 623 624 625 626 627 628
	 */
	if (file->f_path.dentry->d_inode &&
	    file->f_path.dentry->d_inode->i_mapping &&
	    mapping_nrpages(&file->f_path.dentry->d_inode->i_data))
		truncate_inode_pages(file->f_path.dentry->d_inode->i_mapping,
				     0);
	return 0;
}

/*
 * Push all data for a specific file onto permanent storage.
 */
629
static int orangefs_fsync(struct file *file,
630 631 632
		       loff_t start,
		       loff_t end,
		       int datasync)
M
Mike Marshall 已提交
633 634
{
	int ret = -EINVAL;
635 636 637
	struct orangefs_inode_s *orangefs_inode =
		ORANGEFS_I(file->f_path.dentry->d_inode);
	struct orangefs_kernel_op_s *new_op = NULL;
M
Mike Marshall 已提交
638 639 640 641

	/* required call */
	filemap_write_and_wait_range(file->f_mapping, start, end);

642
	new_op = op_alloc(ORANGEFS_VFS_OP_FSYNC);
M
Mike Marshall 已提交
643 644
	if (!new_op)
		return -ENOMEM;
645
	new_op->upcall.req.fsync.refn = orangefs_inode->refn;
M
Mike Marshall 已提交
646 647

	ret = service_operation(new_op,
648
			"orangefs_fsync",
M
Mike Marshall 已提交
649 650 651
			get_interruptible_flag(file->f_path.dentry->d_inode));

	gossip_debug(GOSSIP_FILE_DEBUG,
652
		     "orangefs_fsync got return value of %d\n",
M
Mike Marshall 已提交
653 654 655 656
		     ret);

	op_release(new_op);

657
	orangefs_flush_inode(file->f_path.dentry->d_inode);
M
Mike Marshall 已提交
658 659 660 661 662 663 664 665 666 667 668 669
	return ret;
}

/*
 * Change the file pointer position for an instance of an open file.
 *
 * \note If .llseek is overriden, we must acquire lock as described in
 *       Documentation/filesystems/Locking.
 *
 * Future upgrade could support SEEK_DATA and SEEK_HOLE but would
 * require much changes to the FS
 */
670
static loff_t orangefs_file_llseek(struct file *file, loff_t offset, int origin)
M
Mike Marshall 已提交
671 672 673 674 675
{
	int ret = -EINVAL;
	struct inode *inode = file->f_path.dentry->d_inode;

	if (!inode) {
676
		gossip_err("orangefs_file_llseek: invalid inode (NULL)\n");
M
Mike Marshall 已提交
677 678 679
		return ret;
	}

680
	if (origin == ORANGEFS_SEEK_END) {
M
Mike Marshall 已提交
681 682 683 684 685
		/*
		 * revalidate the inode's file size.
		 * NOTE: We are only interested in file size here,
		 * so we set mask accordingly.
		 */
M
Martin Brandenburg 已提交
686
		ret = orangefs_inode_getattr(inode, ORANGEFS_ATTR_SYS_SIZE, 0);
M
Mike Marshall 已提交
687 688 689 690 691 692
		if (ret) {
			gossip_debug(GOSSIP_FILE_DEBUG,
				     "%s:%s:%d calling make bad inode\n",
				     __FILE__,
				     __func__,
				     __LINE__);
693
			orangefs_make_bad_inode(inode);
M
Mike Marshall 已提交
694 695 696 697 698
			return ret;
		}
	}

	gossip_debug(GOSSIP_FILE_DEBUG,
699
		     "orangefs_file_llseek: offset is %ld | origin is %d"
700
		     " | inode size is %lu\n",
M
Mike Marshall 已提交
701 702 703 704 705 706 707 708 709 710 711
		     (long)offset,
		     origin,
		     (unsigned long)file->f_path.dentry->d_inode->i_size);

	return generic_file_llseek(file, offset, origin);
}

/*
 * Support local locks (locks that only this kernel knows about)
 * if Orangefs was mounted -o local_lock.
 */
712
static int orangefs_lock(struct file *filp, int cmd, struct file_lock *fl)
M
Mike Marshall 已提交
713
{
714
	int rc = -EINVAL;
M
Mike Marshall 已提交
715

716
	if (ORANGEFS_SB(filp->f_inode->i_sb)->flags & ORANGEFS_OPT_LOCAL_LOCK) {
M
Mike Marshall 已提交
717 718 719 720 721 722 723 724 725 726 727
		if (cmd == F_GETLK) {
			rc = 0;
			posix_test_lock(filp, fl);
		} else {
			rc = posix_lock_file(filp, fl, NULL);
		}
	}

	return rc;
}

728 729 730 731 732 733 734 735
/** ORANGEFS implementation of VFS file operations */
const struct file_operations orangefs_file_operations = {
	.llseek		= orangefs_file_llseek,
	.read_iter	= orangefs_file_read_iter,
	.write_iter	= orangefs_file_write_iter,
	.lock		= orangefs_lock,
	.unlocked_ioctl	= orangefs_ioctl,
	.mmap		= orangefs_file_mmap,
M
Mike Marshall 已提交
736
	.open		= generic_file_open,
737 738
	.release	= orangefs_file_release,
	.fsync		= orangefs_fsync,
M
Mike Marshall 已提交
739
};