2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License v.2.
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/pagemap.h>
16 #include <linux/uio.h>
17 #include <linux/blkdev.h>
19 #include <linux/smp_lock.h>
21 #include <linux/gfs2_ondisk.h>
22 #include <asm/semaphore.h>
23 #include <asm/uaccess.h>
26 #include "lm_interface.h"
43 /* "bad" is for NFS support */
44 struct filldir_bad_entry {
46 unsigned int fbe_length;
48 struct gfs2_inum fbe_inum;
49 unsigned int fbe_type;
53 struct gfs2_sbd *fdb_sbd;
55 struct filldir_bad_entry *fdb_entry;
56 unsigned int fdb_entry_num;
57 unsigned int fdb_entry_off;
60 unsigned int fdb_name_size;
61 unsigned int fdb_name_off;
64 /* For regular, non-NFS */
66 struct gfs2_sbd *fdr_sbd;
69 filldir_t fdr_filldir;
74 * Most fields left uninitialised to catch anybody who tries to
75 * use them. f_flags set to prevent file_accessed() from touching
76 * any other part of this. Its use is purely as a flag so that we
77 * know (in readpage()) whether or not do to locking.
79 struct file gfs2_internal_file_sentinal = {
80 .f_flags = O_NOATIME|O_RDONLY,
83 static int gfs2_read_actor(read_descriptor_t *desc, struct page *page,
84 unsigned long offset, unsigned long size)
87 unsigned long count = desc->count;
93 memcpy(desc->arg.buf, kaddr + offset, size);
96 desc->count = count - size;
97 desc->written += size;
98 desc->arg.buf += size;
102 int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
103 char *buf, loff_t *pos, unsigned size)
105 struct inode *inode = ip->i_vnode;
106 read_descriptor_t desc;
111 do_generic_mapping_read(inode->i_mapping, ra_state,
112 &gfs2_internal_file_sentinal, pos, &desc,
114 return desc.written ? desc.written : desc.error;
118 * gfs2_llseek - seek to a location in a file
120 * @offset: the offset
121 * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
123 * SEEK_END requires the glock for the file because it references the
126 * Returns: The new offset, or errno
129 static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
131 struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip;
132 struct gfs2_holder i_gh;
136 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
139 error = remote_llseek(file, offset, origin);
140 gfs2_glock_dq_uninit(&i_gh);
143 error = remote_llseek(file, offset, origin);
149 static ssize_t gfs2_direct_IO_read(struct kiocb *iocb, const struct iovec *iov,
150 loff_t offset, unsigned long nr_segs)
152 struct file *file = iocb->ki_filp;
153 struct address_space *mapping = file->f_mapping;
156 retval = filemap_write_and_wait(mapping);
158 retval = mapping->a_ops->direct_IO(READ, iocb, iov, offset,
165 * __gfs2_file_aio_read - The main GFS2 read function
167 * N.B. This is almost, but not quite the same as __generic_file_aio_read()
168 * the important subtle different being that inode->i_size isn't valid
169 * unless we are holding a lock, and we do this _only_ on the O_DIRECT
170 * path since otherwise locking is done entirely at the page cache
173 static ssize_t __gfs2_file_aio_read(struct kiocb *iocb,
174 const struct iovec *iov,
175 unsigned long nr_segs, loff_t *ppos)
177 struct file *filp = iocb->ki_filp;
178 struct gfs2_inode *ip = filp->f_mapping->host->u.generic_ip;
179 struct gfs2_holder gh;
185 for (seg = 0; seg < nr_segs; seg++) {
186 const struct iovec *iv = &iov[seg];
189 * If any segment has a negative length, or the cumulative
190 * length ever wraps negative then return -EINVAL.
192 count += iv->iov_len;
193 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
195 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
200 count -= iv->iov_len; /* This segment is no good */
204 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
205 if (filp->f_flags & O_DIRECT) {
206 loff_t pos = *ppos, size;
207 struct address_space *mapping;
210 mapping = filp->f_mapping;
211 inode = mapping->host;
214 goto out; /* skip atime */
216 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
217 retval = gfs2_glock_nq_m_atime(1, &gh);
220 if (gfs2_is_stuffed(ip)) {
221 gfs2_glock_dq_m(1, &gh);
222 gfs2_holder_uninit(&gh);
223 goto fallback_to_normal;
225 size = i_size_read(inode);
227 retval = gfs2_direct_IO_read(iocb, iov, pos, nr_segs);
228 if (retval > 0 && !is_sync_kiocb(iocb))
229 retval = -EIOCBQUEUED;
231 *ppos = pos + retval;
234 gfs2_glock_dq_m(1, &gh);
235 gfs2_holder_uninit(&gh);
242 for (seg = 0; seg < nr_segs; seg++) {
243 read_descriptor_t desc;
246 desc.arg.buf = iov[seg].iov_base;
247 desc.count = iov[seg].iov_len;
251 do_generic_file_read(filp,ppos,&desc,file_read_actor);
252 retval += desc.written;
254 retval = retval ?: desc.error;
264 * gfs2_read - Read bytes from a file
265 * @file: The file to read from
266 * @buf: The buffer to copy into
267 * @size: The amount of data requested
268 * @offset: The current file offset
270 * Outputs: Offset - updated according to number of bytes read
272 * Returns: The number of bytes read, errno on failure
275 static ssize_t gfs2_read(struct file *filp, char __user *buf, size_t size,
278 struct iovec local_iov = { .iov_base = buf, .iov_len = size };
282 init_sync_kiocb(&kiocb, filp);
283 ret = __gfs2_file_aio_read(&kiocb, &local_iov, 1, offset);
284 if (-EIOCBQUEUED == ret)
285 ret = wait_on_sync_kiocb(&kiocb);
289 static ssize_t gfs2_file_readv(struct file *filp, const struct iovec *iov,
290 unsigned long nr_segs, loff_t *ppos)
295 init_sync_kiocb(&kiocb, filp);
296 ret = __gfs2_file_aio_read(&kiocb, iov, nr_segs, ppos);
297 if (-EIOCBQUEUED == ret)
298 ret = wait_on_sync_kiocb(&kiocb);
302 static ssize_t gfs2_file_aio_read(struct kiocb *iocb, char __user *buf,
303 size_t count, loff_t pos)
305 struct iovec local_iov = { .iov_base = buf, .iov_len = count };
307 BUG_ON(iocb->ki_pos != pos);
308 return __gfs2_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos);
313 * filldir_reg_func - Report a directory entry to the caller of gfs2_dir_read()
314 * @opaque: opaque data used by the function
315 * @name: the name of the directory entry
316 * @length: the length of the name
317 * @offset: the entry's offset in the directory
318 * @inum: the inode number the entry points to
319 * @type: the type of inode the entry points to
321 * Returns: 0 on success, 1 if buffer full
324 static int filldir_reg_func(void *opaque, const char *name, unsigned int length,
325 uint64_t offset, struct gfs2_inum *inum,
328 struct filldir_reg *fdr = (struct filldir_reg *)opaque;
329 struct gfs2_sbd *sdp = fdr->fdr_sbd;
332 error = fdr->fdr_filldir(fdr->fdr_opaque, name, length, offset,
333 inum->no_formal_ino, type);
337 if (fdr->fdr_prefetch && !(length == 1 && *name == '.')) {
338 gfs2_glock_prefetch_num(sdp,
339 inum->no_addr, &gfs2_inode_glops,
340 LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
341 gfs2_glock_prefetch_num(sdp,
342 inum->no_addr, &gfs2_iopen_glops,
343 LM_ST_SHARED, LM_FLAG_TRY);
350 * readdir_reg - Read directory entries from a directory
351 * @file: The directory to read from
352 * @dirent: Buffer for dirents
353 * @filldir: Function used to do the copying
358 static int readdir_reg(struct file *file, void *dirent, filldir_t filldir)
360 struct gfs2_inode *dip = file->f_mapping->host->u.generic_ip;
361 struct filldir_reg fdr;
362 struct gfs2_holder d_gh;
363 uint64_t offset = file->f_pos;
366 fdr.fdr_sbd = dip->i_sbd;
367 fdr.fdr_prefetch = 1;
368 fdr.fdr_filldir = filldir;
369 fdr.fdr_opaque = dirent;
371 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
372 error = gfs2_glock_nq_atime(&d_gh);
374 gfs2_holder_uninit(&d_gh);
378 error = gfs2_dir_read(dip, &offset, &fdr, filldir_reg_func);
380 gfs2_glock_dq_uninit(&d_gh);
382 file->f_pos = offset;
388 * filldir_bad_func - Report a directory entry to the caller of gfs2_dir_read()
389 * @opaque: opaque data used by the function
390 * @name: the name of the directory entry
391 * @length: the length of the name
392 * @offset: the entry's offset in the directory
393 * @inum: the inode number the entry points to
394 * @type: the type of inode the entry points to
396 * For supporting NFS.
398 * Returns: 0 on success, 1 if buffer full
401 static int filldir_bad_func(void *opaque, const char *name, unsigned int length,
402 uint64_t offset, struct gfs2_inum *inum,
405 struct filldir_bad *fdb = (struct filldir_bad *)opaque;
406 struct gfs2_sbd *sdp = fdb->fdb_sbd;
407 struct filldir_bad_entry *fbe;
409 if (fdb->fdb_entry_off == fdb->fdb_entry_num ||
410 fdb->fdb_name_off + length > fdb->fdb_name_size)
413 fbe = &fdb->fdb_entry[fdb->fdb_entry_off];
414 fbe->fbe_name = fdb->fdb_name + fdb->fdb_name_off;
415 memcpy(fbe->fbe_name, name, length);
416 fbe->fbe_length = length;
417 fbe->fbe_offset = offset;
418 fbe->fbe_inum = *inum;
419 fbe->fbe_type = type;
421 fdb->fdb_entry_off++;
422 fdb->fdb_name_off += length;
424 if (!(length == 1 && *name == '.')) {
425 gfs2_glock_prefetch_num(sdp,
426 inum->no_addr, &gfs2_inode_glops,
427 LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
428 gfs2_glock_prefetch_num(sdp,
429 inum->no_addr, &gfs2_iopen_glops,
430 LM_ST_SHARED, LM_FLAG_TRY);
437 * readdir_bad - Read directory entries from a directory
438 * @file: The directory to read from
439 * @dirent: Buffer for dirents
440 * @filldir: Function used to do the copying
442 * For supporting NFS.
447 static int readdir_bad(struct file *file, void *dirent, filldir_t filldir)
449 struct gfs2_inode *dip = file->f_mapping->host->u.generic_ip;
450 struct gfs2_sbd *sdp = dip->i_sbd;
451 struct filldir_reg fdr;
452 unsigned int entries, size;
453 struct filldir_bad *fdb;
454 struct gfs2_holder d_gh;
455 uint64_t offset = file->f_pos;
457 struct filldir_bad_entry *fbe;
460 entries = gfs2_tune_get(sdp, gt_entries_per_readdir);
461 size = sizeof(struct filldir_bad) +
462 entries * (sizeof(struct filldir_bad_entry) + GFS2_FAST_NAME_SIZE);
464 fdb = kzalloc(size, GFP_KERNEL);
469 fdb->fdb_entry = (struct filldir_bad_entry *)(fdb + 1);
470 fdb->fdb_entry_num = entries;
471 fdb->fdb_name = ((char *)fdb) + sizeof(struct filldir_bad) +
472 entries * sizeof(struct filldir_bad_entry);
473 fdb->fdb_name_size = entries * GFS2_FAST_NAME_SIZE;
475 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
476 error = gfs2_glock_nq_atime(&d_gh);
478 gfs2_holder_uninit(&d_gh);
482 error = gfs2_dir_read(dip, &offset, fdb, filldir_bad_func);
484 gfs2_glock_dq_uninit(&d_gh);
487 fdr.fdr_prefetch = 0;
488 fdr.fdr_filldir = filldir;
489 fdr.fdr_opaque = dirent;
491 for (x = 0; x < fdb->fdb_entry_off; x++) {
492 fbe = &fdb->fdb_entry[x];
494 error = filldir_reg_func(&fdr,
495 fbe->fbe_name, fbe->fbe_length,
497 &fbe->fbe_inum, fbe->fbe_type);
499 file->f_pos = fbe->fbe_offset;
505 file->f_pos = offset;
514 * gfs2_readdir - Read directory entries from a directory
515 * @file: The directory to read from
516 * @dirent: Buffer for dirents
517 * @filldir: Function used to do the copying
522 static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
526 if (strcmp(current->comm, "nfsd") != 0)
527 error = readdir_reg(file, dirent, filldir);
529 error = readdir_bad(file, dirent, filldir);
536 * @file: The file to map
537 * @vma: The VMA which described the mapping
539 * Returns: 0 or error code
542 static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
544 struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip;
545 struct gfs2_holder i_gh;
548 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
549 error = gfs2_glock_nq_atime(&i_gh);
551 gfs2_holder_uninit(&i_gh);
555 /* This is VM_MAYWRITE instead of VM_WRITE because a call
556 to mprotect() can turn on VM_WRITE later. */
558 if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
559 (VM_MAYSHARE | VM_MAYWRITE))
560 vma->vm_ops = &gfs2_vm_ops_sharewrite;
562 vma->vm_ops = &gfs2_vm_ops_private;
564 gfs2_glock_dq_uninit(&i_gh);
570 * gfs2_open - open a file
571 * @inode: the inode to open
572 * @file: the struct file for this opening
577 static int gfs2_open(struct inode *inode, struct file *file)
579 struct gfs2_inode *ip = inode->u.generic_ip;
580 struct gfs2_holder i_gh;
581 struct gfs2_file *fp;
584 fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
588 mutex_init(&fp->f_fl_mutex);
593 gfs2_assert_warn(ip->i_sbd, !file->private_data);
594 file->private_data = fp;
596 if (S_ISREG(ip->i_di.di_mode)) {
597 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
602 if (!(file->f_flags & O_LARGEFILE) &&
603 ip->i_di.di_size > MAX_NON_LFS) {
608 /* Listen to the Direct I/O flag */
610 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
611 file->f_flags |= O_DIRECT;
613 gfs2_glock_dq_uninit(&i_gh);
619 gfs2_glock_dq_uninit(&i_gh);
622 file->private_data = NULL;
629 * gfs2_close - called to close a struct file
630 * @inode: the inode the struct file belongs to
631 * @file: the struct file being closed
636 static int gfs2_close(struct inode *inode, struct file *file)
638 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
639 struct gfs2_file *fp;
641 fp = file->private_data;
642 file->private_data = NULL;
644 if (gfs2_assert_warn(sdp, fp))
653 * gfs2_fsync - sync the dirty data for a file (across the cluster)
654 * @file: the file that points to the dentry (we ignore this)
655 * @dentry: the dentry that points to the inode to sync
660 static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
662 struct gfs2_inode *ip = dentry->d_inode->u.generic_ip;
664 gfs2_log_flush_glock(ip->i_gl);
670 * gfs2_lock - acquire/release a posix lock on a file
671 * @file: the file pointer
672 * @cmd: either modify or retrieve lock state, possibly wait
673 * @fl: type and range of lock
678 static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
680 struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip;
681 struct gfs2_sbd *sdp = ip->i_sbd;
682 struct lm_lockname name =
683 { .ln_number = ip->i_num.no_addr,
684 .ln_type = LM_TYPE_PLOCK };
686 if (!(fl->fl_flags & FL_POSIX))
688 if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
691 if (sdp->sd_args.ar_localflocks) {
693 struct file_lock *tmp;
695 tmp = posix_test_lock(file, fl);
696 fl->fl_type = F_UNLCK;
698 memcpy(fl, tmp, sizeof(struct file_lock));
704 error = posix_lock_file_wait(file, fl);
711 return gfs2_lm_plock_get(sdp, &name, file, fl);
712 else if (fl->fl_type == F_UNLCK)
713 return gfs2_lm_punlock(sdp, &name, file, fl);
715 return gfs2_lm_plock(sdp, &name, file, cmd, fl);
719 * gfs2_sendfile - Send bytes to a file or socket
720 * @in_file: The file to read from
721 * @out_file: The file to write to
722 * @count: The amount of data
723 * @offset: The beginning file offset
725 * Outputs: offset - updated according to number of bytes read
727 * Returns: The number of bytes sent, errno on failure
730 static ssize_t gfs2_sendfile(struct file *in_file, loff_t *offset, size_t count,
731 read_actor_t actor, void *target)
733 return generic_file_sendfile(in_file, offset, count, actor, target);
736 static int do_flock(struct file *file, int cmd, struct file_lock *fl)
738 struct gfs2_file *fp = file->private_data;
739 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
740 struct gfs2_inode *ip = fp->f_inode;
741 struct gfs2_glock *gl;
746 state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
747 flags = ((IS_SETLKW(cmd)) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
749 mutex_lock(&fp->f_fl_mutex);
753 if (fl_gh->gh_state == state)
756 flock_lock_file_wait(file,
757 &(struct file_lock){.fl_type = F_UNLCK});
758 gfs2_glock_dq_uninit(fl_gh);
760 error = gfs2_glock_get(ip->i_sbd,
761 ip->i_num.no_addr, &gfs2_flock_glops,
767 gfs2_holder_init(gl, state, flags, fl_gh);
770 error = gfs2_glock_nq(fl_gh);
772 gfs2_holder_uninit(fl_gh);
773 if (error == GLR_TRYFAILED)
776 error = flock_lock_file_wait(file, fl);
777 gfs2_assert_warn(ip->i_sbd, !error);
781 mutex_unlock(&fp->f_fl_mutex);
786 static void do_unflock(struct file *file, struct file_lock *fl)
788 struct gfs2_file *fp = file->private_data;
789 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
791 mutex_lock(&fp->f_fl_mutex);
792 flock_lock_file_wait(file, fl);
794 gfs2_glock_dq_uninit(fl_gh);
795 mutex_unlock(&fp->f_fl_mutex);
799 * gfs2_flock - acquire/release a flock lock on a file
800 * @file: the file pointer
801 * @cmd: either modify or retrieve lock state, possibly wait
802 * @fl: type and range of lock
807 static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
809 struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip;
810 struct gfs2_sbd *sdp = ip->i_sbd;
812 if (!(fl->fl_flags & FL_FLOCK))
814 if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
817 if (sdp->sd_args.ar_localflocks)
818 return flock_lock_file_wait(file, fl);
820 if (fl->fl_type == F_UNLCK) {
821 do_unflock(file, fl);
824 return do_flock(file, cmd, fl);
827 struct file_operations gfs2_file_fops = {
828 .llseek = gfs2_llseek,
830 .readv = gfs2_file_readv,
831 .aio_read = gfs2_file_aio_read,
832 .write = generic_file_write,
833 .writev = generic_file_writev,
834 .aio_write = generic_file_aio_write,
837 .release = gfs2_close,
840 .sendfile = gfs2_sendfile,
844 struct file_operations gfs2_dir_fops = {
845 .readdir = gfs2_readdir,
847 .release = gfs2_close,