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>
20 #include <linux/gfs2_ioctl.h>
22 #include <linux/gfs2_ondisk.h>
23 #include <asm/semaphore.h>
24 #include <asm/uaccess.h>
27 #include "lm_interface.h"
44 /* "bad" is for NFS support */
45 struct filldir_bad_entry {
47 unsigned int fbe_length;
49 struct gfs2_inum fbe_inum;
50 unsigned int fbe_type;
54 struct gfs2_sbd *fdb_sbd;
56 struct filldir_bad_entry *fdb_entry;
57 unsigned int fdb_entry_num;
58 unsigned int fdb_entry_off;
61 unsigned int fdb_name_size;
62 unsigned int fdb_name_off;
65 /* For regular, non-NFS */
67 struct gfs2_sbd *fdr_sbd;
70 filldir_t fdr_filldir;
75 * Most fields left uninitialised to catch anybody who tries to
76 * use them. f_flags set to prevent file_accessed() from touching
77 * any other part of this. Its use is purely as a flag so that we
78 * know (in readpage()) whether or not do to locking.
80 struct file gfs2_internal_file_sentinal = {
81 .f_flags = O_NOATIME|O_RDONLY,
84 static int gfs2_read_actor(read_descriptor_t *desc, struct page *page,
85 unsigned long offset, unsigned long size)
88 unsigned long count = desc->count;
94 memcpy(desc->arg.buf, kaddr + offset, size);
97 desc->count = count - size;
98 desc->written += size;
99 desc->arg.buf += size;
103 int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
104 char *buf, loff_t *pos, unsigned size)
106 struct inode *inode = ip->i_vnode;
107 read_descriptor_t desc;
112 do_generic_mapping_read(inode->i_mapping, ra_state,
113 &gfs2_internal_file_sentinal, pos, &desc,
115 return desc.written ? desc.written : desc.error;
119 * gfs2_llseek - seek to a location in a file
121 * @offset: the offset
122 * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
124 * SEEK_END requires the glock for the file because it references the
127 * Returns: The new offset, or errno
130 static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
132 struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip;
133 struct gfs2_holder i_gh;
137 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
140 error = remote_llseek(file, offset, origin);
141 gfs2_glock_dq_uninit(&i_gh);
144 error = remote_llseek(file, offset, origin);
150 static ssize_t gfs2_direct_IO_read(struct kiocb *iocb, const struct iovec *iov,
151 loff_t offset, unsigned long nr_segs)
153 struct file *file = iocb->ki_filp;
154 struct address_space *mapping = file->f_mapping;
157 retval = filemap_write_and_wait(mapping);
159 retval = mapping->a_ops->direct_IO(READ, iocb, iov, offset,
166 * __gfs2_file_aio_read - The main GFS2 read function
168 * N.B. This is almost, but not quite the same as __generic_file_aio_read()
169 * the important subtle different being that inode->i_size isn't valid
170 * unless we are holding a lock, and we do this _only_ on the O_DIRECT
171 * path since otherwise locking is done entirely at the page cache
174 static ssize_t __gfs2_file_aio_read(struct kiocb *iocb,
175 const struct iovec *iov,
176 unsigned long nr_segs, loff_t *ppos)
178 struct file *filp = iocb->ki_filp;
179 struct gfs2_inode *ip = filp->f_mapping->host->u.generic_ip;
180 struct gfs2_holder gh;
186 for (seg = 0; seg < nr_segs; seg++) {
187 const struct iovec *iv = &iov[seg];
190 * If any segment has a negative length, or the cumulative
191 * length ever wraps negative then return -EINVAL.
193 count += iv->iov_len;
194 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
196 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
201 count -= iv->iov_len; /* This segment is no good */
205 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
206 if (filp->f_flags & O_DIRECT) {
207 loff_t pos = *ppos, size;
208 struct address_space *mapping;
211 mapping = filp->f_mapping;
212 inode = mapping->host;
215 goto out; /* skip atime */
217 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
218 retval = gfs2_glock_nq_m_atime(1, &gh);
221 if (gfs2_is_stuffed(ip)) {
222 gfs2_glock_dq_m(1, &gh);
223 gfs2_holder_uninit(&gh);
224 goto fallback_to_normal;
226 size = i_size_read(inode);
228 retval = gfs2_direct_IO_read(iocb, iov, pos, nr_segs);
229 if (retval > 0 && !is_sync_kiocb(iocb))
230 retval = -EIOCBQUEUED;
232 *ppos = pos + retval;
235 gfs2_glock_dq_m(1, &gh);
236 gfs2_holder_uninit(&gh);
243 for (seg = 0; seg < nr_segs; seg++) {
244 read_descriptor_t desc;
247 desc.arg.buf = iov[seg].iov_base;
248 desc.count = iov[seg].iov_len;
252 do_generic_file_read(filp,ppos,&desc,file_read_actor);
253 retval += desc.written;
255 retval = retval ?: desc.error;
265 * gfs2_read - Read bytes from a file
266 * @file: The file to read from
267 * @buf: The buffer to copy into
268 * @size: The amount of data requested
269 * @offset: The current file offset
271 * Outputs: Offset - updated according to number of bytes read
273 * Returns: The number of bytes read, errno on failure
276 static ssize_t gfs2_read(struct file *filp, char __user *buf, size_t size,
279 struct iovec local_iov = { .iov_base = buf, .iov_len = size };
283 init_sync_kiocb(&kiocb, filp);
284 ret = __gfs2_file_aio_read(&kiocb, &local_iov, 1, offset);
285 if (-EIOCBQUEUED == ret)
286 ret = wait_on_sync_kiocb(&kiocb);
290 static ssize_t gfs2_file_readv(struct file *filp, const struct iovec *iov,
291 unsigned long nr_segs, loff_t *ppos)
296 init_sync_kiocb(&kiocb, filp);
297 ret = __gfs2_file_aio_read(&kiocb, iov, nr_segs, ppos);
298 if (-EIOCBQUEUED == ret)
299 ret = wait_on_sync_kiocb(&kiocb);
303 static ssize_t gfs2_file_aio_read(struct kiocb *iocb, char __user *buf,
304 size_t count, loff_t pos)
306 struct iovec local_iov = { .iov_base = buf, .iov_len = count };
308 BUG_ON(iocb->ki_pos != pos);
309 return __gfs2_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos);
314 * filldir_reg_func - Report a directory entry to the caller of gfs2_dir_read()
315 * @opaque: opaque data used by the function
316 * @name: the name of the directory entry
317 * @length: the length of the name
318 * @offset: the entry's offset in the directory
319 * @inum: the inode number the entry points to
320 * @type: the type of inode the entry points to
322 * Returns: 0 on success, 1 if buffer full
325 static int filldir_reg_func(void *opaque, const char *name, unsigned int length,
326 uint64_t offset, struct gfs2_inum *inum,
329 struct filldir_reg *fdr = (struct filldir_reg *)opaque;
330 struct gfs2_sbd *sdp = fdr->fdr_sbd;
333 error = fdr->fdr_filldir(fdr->fdr_opaque, name, length, offset,
334 inum->no_formal_ino, type);
338 if (fdr->fdr_prefetch && !(length == 1 && *name == '.')) {
339 gfs2_glock_prefetch_num(sdp,
340 inum->no_addr, &gfs2_inode_glops,
341 LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
342 gfs2_glock_prefetch_num(sdp,
343 inum->no_addr, &gfs2_iopen_glops,
344 LM_ST_SHARED, LM_FLAG_TRY);
351 * readdir_reg - Read directory entries from a directory
352 * @file: The directory to read from
353 * @dirent: Buffer for dirents
354 * @filldir: Function used to do the copying
359 static int readdir_reg(struct file *file, void *dirent, filldir_t filldir)
361 struct gfs2_inode *dip = file->f_mapping->host->u.generic_ip;
362 struct filldir_reg fdr;
363 struct gfs2_holder d_gh;
364 uint64_t offset = file->f_pos;
367 fdr.fdr_sbd = dip->i_sbd;
368 fdr.fdr_prefetch = 1;
369 fdr.fdr_filldir = filldir;
370 fdr.fdr_opaque = dirent;
372 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
373 error = gfs2_glock_nq_atime(&d_gh);
375 gfs2_holder_uninit(&d_gh);
379 error = gfs2_dir_read(dip, &offset, &fdr, filldir_reg_func);
381 gfs2_glock_dq_uninit(&d_gh);
383 file->f_pos = offset;
389 * filldir_bad_func - Report a directory entry to the caller of gfs2_dir_read()
390 * @opaque: opaque data used by the function
391 * @name: the name of the directory entry
392 * @length: the length of the name
393 * @offset: the entry's offset in the directory
394 * @inum: the inode number the entry points to
395 * @type: the type of inode the entry points to
397 * For supporting NFS.
399 * Returns: 0 on success, 1 if buffer full
402 static int filldir_bad_func(void *opaque, const char *name, unsigned int length,
403 uint64_t offset, struct gfs2_inum *inum,
406 struct filldir_bad *fdb = (struct filldir_bad *)opaque;
407 struct gfs2_sbd *sdp = fdb->fdb_sbd;
408 struct filldir_bad_entry *fbe;
410 if (fdb->fdb_entry_off == fdb->fdb_entry_num ||
411 fdb->fdb_name_off + length > fdb->fdb_name_size)
414 fbe = &fdb->fdb_entry[fdb->fdb_entry_off];
415 fbe->fbe_name = fdb->fdb_name + fdb->fdb_name_off;
416 memcpy(fbe->fbe_name, name, length);
417 fbe->fbe_length = length;
418 fbe->fbe_offset = offset;
419 fbe->fbe_inum = *inum;
420 fbe->fbe_type = type;
422 fdb->fdb_entry_off++;
423 fdb->fdb_name_off += length;
425 if (!(length == 1 && *name == '.')) {
426 gfs2_glock_prefetch_num(sdp,
427 inum->no_addr, &gfs2_inode_glops,
428 LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
429 gfs2_glock_prefetch_num(sdp,
430 inum->no_addr, &gfs2_iopen_glops,
431 LM_ST_SHARED, LM_FLAG_TRY);
438 * readdir_bad - Read directory entries from a directory
439 * @file: The directory to read from
440 * @dirent: Buffer for dirents
441 * @filldir: Function used to do the copying
443 * For supporting NFS.
448 static int readdir_bad(struct file *file, void *dirent, filldir_t filldir)
450 struct gfs2_inode *dip = file->f_mapping->host->u.generic_ip;
451 struct gfs2_sbd *sdp = dip->i_sbd;
452 struct filldir_reg fdr;
453 unsigned int entries, size;
454 struct filldir_bad *fdb;
455 struct gfs2_holder d_gh;
456 uint64_t offset = file->f_pos;
458 struct filldir_bad_entry *fbe;
461 entries = gfs2_tune_get(sdp, gt_entries_per_readdir);
462 size = sizeof(struct filldir_bad) +
463 entries * (sizeof(struct filldir_bad_entry) + GFS2_FAST_NAME_SIZE);
465 fdb = kzalloc(size, GFP_KERNEL);
470 fdb->fdb_entry = (struct filldir_bad_entry *)(fdb + 1);
471 fdb->fdb_entry_num = entries;
472 fdb->fdb_name = ((char *)fdb) + sizeof(struct filldir_bad) +
473 entries * sizeof(struct filldir_bad_entry);
474 fdb->fdb_name_size = entries * GFS2_FAST_NAME_SIZE;
476 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
477 error = gfs2_glock_nq_atime(&d_gh);
479 gfs2_holder_uninit(&d_gh);
483 error = gfs2_dir_read(dip, &offset, fdb, filldir_bad_func);
485 gfs2_glock_dq_uninit(&d_gh);
488 fdr.fdr_prefetch = 0;
489 fdr.fdr_filldir = filldir;
490 fdr.fdr_opaque = dirent;
492 for (x = 0; x < fdb->fdb_entry_off; x++) {
493 fbe = &fdb->fdb_entry[x];
495 error = filldir_reg_func(&fdr,
496 fbe->fbe_name, fbe->fbe_length,
498 &fbe->fbe_inum, fbe->fbe_type);
500 file->f_pos = fbe->fbe_offset;
506 file->f_pos = offset;
515 * gfs2_readdir - Read directory entries from a directory
516 * @file: The directory to read from
517 * @dirent: Buffer for dirents
518 * @filldir: Function used to do the copying
523 static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
527 if (strcmp(current->comm, "nfsd") != 0)
528 error = readdir_reg(file, dirent, filldir);
530 error = readdir_bad(file, dirent, filldir);
535 static int gfs2_ioctl_flags(struct gfs2_inode *ip, unsigned int cmd,
538 unsigned int lmode = (cmd == GFS2_IOCTL_SETFLAGS) ?
539 LM_ST_EXCLUSIVE : LM_ST_SHARED;
540 struct buffer_head *dibh;
541 struct gfs2_holder i_gh;
543 __u32 flags = 0, change;
545 if (cmd == GFS2_IOCTL_SETFLAGS) {
546 error = get_user(flags, (__u32 __user *)arg);
551 error = gfs2_glock_nq_init(ip->i_gl, lmode, 0, &i_gh);
555 if (cmd == GFS2_IOCTL_SETFLAGS) {
556 change = flags ^ ip->i_di.di_flags;
558 if (change & (GFS2_DIF_IMMUTABLE|GFS2_DIF_APPENDONLY)) {
559 if (!capable(CAP_LINUX_IMMUTABLE))
563 if (flags & (GFS2_DIF_JDATA|GFS2_DIF_DIRECTIO)) {
564 if (!S_ISREG(ip->i_di.di_mode))
568 (GFS2_DIF_INHERIT_JDATA|GFS2_DIF_INHERIT_DIRECTIO)) {
569 if (!S_ISDIR(ip->i_di.di_mode))
573 error = gfs2_trans_begin(ip->i_sbd, RES_DINODE, 0);
577 error = gfs2_meta_inode_buffer(ip, &dibh);
581 ip->i_di.di_flags = flags;
583 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
584 gfs2_dinode_out(&ip->i_di, dibh->b_data);
589 gfs2_trans_end(ip->i_sbd);
591 flags = ip->i_di.di_flags;
594 gfs2_glock_dq_uninit(&i_gh);
595 if (cmd == GFS2_IOCTL_GETFLAGS) {
596 if (put_user(flags, (__u32 __user *)arg))
603 * gfs2_ioctl - do an ioctl on a file
605 * @file: the file pointer
606 * @cmd: the ioctl command
612 static int gfs2_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
615 struct gfs2_inode *ip = inode->u.generic_ip;
618 case GFS2_IOCTL_SETFLAGS:
619 case GFS2_IOCTL_GETFLAGS:
620 return gfs2_ioctl_flags(ip, cmd, arg);
629 * @file: The file to map
630 * @vma: The VMA which described the mapping
632 * Returns: 0 or error code
635 static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
637 struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip;
638 struct gfs2_holder i_gh;
641 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
642 error = gfs2_glock_nq_atime(&i_gh);
644 gfs2_holder_uninit(&i_gh);
648 /* This is VM_MAYWRITE instead of VM_WRITE because a call
649 to mprotect() can turn on VM_WRITE later. */
651 if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
652 (VM_MAYSHARE | VM_MAYWRITE))
653 vma->vm_ops = &gfs2_vm_ops_sharewrite;
655 vma->vm_ops = &gfs2_vm_ops_private;
657 gfs2_glock_dq_uninit(&i_gh);
663 * gfs2_open - open a file
664 * @inode: the inode to open
665 * @file: the struct file for this opening
670 static int gfs2_open(struct inode *inode, struct file *file)
672 struct gfs2_inode *ip = inode->u.generic_ip;
673 struct gfs2_holder i_gh;
674 struct gfs2_file *fp;
677 fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
681 mutex_init(&fp->f_fl_mutex);
686 gfs2_assert_warn(ip->i_sbd, !file->private_data);
687 file->private_data = fp;
689 if (S_ISREG(ip->i_di.di_mode)) {
690 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
695 if (!(file->f_flags & O_LARGEFILE) &&
696 ip->i_di.di_size > MAX_NON_LFS) {
701 /* Listen to the Direct I/O flag */
703 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
704 file->f_flags |= O_DIRECT;
706 gfs2_glock_dq_uninit(&i_gh);
712 gfs2_glock_dq_uninit(&i_gh);
715 file->private_data = NULL;
722 * gfs2_close - called to close a struct file
723 * @inode: the inode the struct file belongs to
724 * @file: the struct file being closed
729 static int gfs2_close(struct inode *inode, struct file *file)
731 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
732 struct gfs2_file *fp;
734 fp = file->private_data;
735 file->private_data = NULL;
737 if (gfs2_assert_warn(sdp, fp))
746 * gfs2_fsync - sync the dirty data for a file (across the cluster)
747 * @file: the file that points to the dentry (we ignore this)
748 * @dentry: the dentry that points to the inode to sync
753 static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
755 struct gfs2_inode *ip = dentry->d_inode->u.generic_ip;
757 gfs2_log_flush_glock(ip->i_gl);
763 * gfs2_lock - acquire/release a posix lock on a file
764 * @file: the file pointer
765 * @cmd: either modify or retrieve lock state, possibly wait
766 * @fl: type and range of lock
771 static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
773 struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip;
774 struct gfs2_sbd *sdp = ip->i_sbd;
775 struct lm_lockname name =
776 { .ln_number = ip->i_num.no_addr,
777 .ln_type = LM_TYPE_PLOCK };
779 if (!(fl->fl_flags & FL_POSIX))
781 if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
784 if (sdp->sd_args.ar_localflocks) {
786 struct file_lock *tmp;
788 tmp = posix_test_lock(file, fl);
789 fl->fl_type = F_UNLCK;
791 memcpy(fl, tmp, sizeof(struct file_lock));
797 error = posix_lock_file_wait(file, fl);
804 return gfs2_lm_plock_get(sdp, &name, file, fl);
805 else if (fl->fl_type == F_UNLCK)
806 return gfs2_lm_punlock(sdp, &name, file, fl);
808 return gfs2_lm_plock(sdp, &name, file, cmd, fl);
812 * gfs2_sendfile - Send bytes to a file or socket
813 * @in_file: The file to read from
814 * @out_file: The file to write to
815 * @count: The amount of data
816 * @offset: The beginning file offset
818 * Outputs: offset - updated according to number of bytes read
820 * Returns: The number of bytes sent, errno on failure
823 static ssize_t gfs2_sendfile(struct file *in_file, loff_t *offset, size_t count,
824 read_actor_t actor, void *target)
826 return generic_file_sendfile(in_file, offset, count, actor, target);
829 static int do_flock(struct file *file, int cmd, struct file_lock *fl)
831 struct gfs2_file *fp = file->private_data;
832 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
833 struct gfs2_inode *ip = fp->f_inode;
834 struct gfs2_glock *gl;
839 state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
840 flags = ((IS_SETLKW(cmd)) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
842 mutex_lock(&fp->f_fl_mutex);
846 if (fl_gh->gh_state == state)
849 flock_lock_file_wait(file,
850 &(struct file_lock){.fl_type = F_UNLCK});
851 gfs2_glock_dq_uninit(fl_gh);
853 error = gfs2_glock_get(ip->i_sbd,
854 ip->i_num.no_addr, &gfs2_flock_glops,
860 gfs2_holder_init(gl, state, flags, fl_gh);
863 error = gfs2_glock_nq(fl_gh);
865 gfs2_holder_uninit(fl_gh);
866 if (error == GLR_TRYFAILED)
869 error = flock_lock_file_wait(file, fl);
870 gfs2_assert_warn(ip->i_sbd, !error);
874 mutex_unlock(&fp->f_fl_mutex);
879 static void do_unflock(struct file *file, struct file_lock *fl)
881 struct gfs2_file *fp = file->private_data;
882 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
884 mutex_lock(&fp->f_fl_mutex);
885 flock_lock_file_wait(file, fl);
887 gfs2_glock_dq_uninit(fl_gh);
888 mutex_unlock(&fp->f_fl_mutex);
892 * gfs2_flock - acquire/release a flock lock on a file
893 * @file: the file pointer
894 * @cmd: either modify or retrieve lock state, possibly wait
895 * @fl: type and range of lock
900 static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
902 struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip;
903 struct gfs2_sbd *sdp = ip->i_sbd;
905 if (!(fl->fl_flags & FL_FLOCK))
907 if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
910 if (sdp->sd_args.ar_localflocks)
911 return flock_lock_file_wait(file, fl);
913 if (fl->fl_type == F_UNLCK) {
914 do_unflock(file, fl);
917 return do_flock(file, cmd, fl);
920 struct file_operations gfs2_file_fops = {
921 .llseek = gfs2_llseek,
923 .readv = gfs2_file_readv,
924 .aio_read = gfs2_file_aio_read,
925 .write = generic_file_write,
926 .writev = generic_file_writev,
927 .aio_write = generic_file_aio_write,
931 .release = gfs2_close,
934 .sendfile = gfs2_sendfile,
938 struct file_operations gfs2_dir_fops = {
939 .readdir = gfs2_readdir,
942 .release = gfs2_close,