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 <asm/semaphore.h>
23 #include <asm/uaccess.h>
40 /* "bad" is for NFS support */
41 struct filldir_bad_entry {
43 unsigned int fbe_length;
45 struct gfs2_inum fbe_inum;
46 unsigned int fbe_type;
50 struct gfs2_sbd *fdb_sbd;
52 struct filldir_bad_entry *fdb_entry;
53 unsigned int fdb_entry_num;
54 unsigned int fdb_entry_off;
57 unsigned int fdb_name_size;
58 unsigned int fdb_name_off;
61 /* For regular, non-NFS */
63 struct gfs2_sbd *fdr_sbd;
66 filldir_t fdr_filldir;
71 * Most fields left uninitialised to catch anybody who tries to
72 * use them. f_flags set to prevent file_accessed() from touching
73 * any other part of this. Its use is purely as a flag so that we
74 * know (in readpage()) whether or not do to locking.
76 struct file gfs2_internal_file_sentinal = {
77 .f_flags = O_NOATIME|O_RDONLY,
80 static int gfs2_read_actor(read_descriptor_t *desc, struct page *page,
81 unsigned long offset, unsigned long size)
84 unsigned long count = desc->count;
90 memcpy(desc->arg.buf, kaddr + offset, size);
93 desc->count = count - size;
94 desc->written += size;
95 desc->arg.buf += size;
99 int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
100 char *buf, loff_t *pos, unsigned size)
102 struct inode *inode = ip->i_vnode;
103 read_descriptor_t desc;
108 do_generic_mapping_read(inode->i_mapping, ra_state,
109 &gfs2_internal_file_sentinal, pos, &desc,
111 return desc.written ? desc.written : desc.error;
115 * gfs2_llseek - seek to a location in a file
117 * @offset: the offset
118 * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
120 * SEEK_END requires the glock for the file because it references the
123 * Returns: The new offset, or errno
126 static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
128 struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
129 struct gfs2_holder i_gh;
133 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
136 error = remote_llseek(file, offset, origin);
137 gfs2_glock_dq_uninit(&i_gh);
140 error = remote_llseek(file, offset, origin);
146 static ssize_t gfs2_direct_IO_read(struct kiocb *iocb, const struct iovec *iov,
147 loff_t offset, unsigned long nr_segs)
149 struct file *file = iocb->ki_filp;
150 struct address_space *mapping = file->f_mapping;
153 retval = filemap_write_and_wait(mapping);
155 retval = mapping->a_ops->direct_IO(READ, iocb, iov, offset,
162 * __gfs2_file_aio_read - The main GFS2 read function
164 * N.B. This is almost, but not quite the same as __generic_file_aio_read()
165 * the important subtle different being that inode->i_size isn't valid
166 * unless we are holding a lock, and we do this _only_ on the O_DIRECT
167 * path since otherwise locking is done entirely at the page cache
170 static ssize_t __gfs2_file_aio_read(struct kiocb *iocb,
171 const struct iovec *iov,
172 unsigned long nr_segs, loff_t *ppos)
174 struct file *filp = iocb->ki_filp;
175 struct gfs2_inode *ip = get_v2ip(filp->f_mapping->host);
176 struct gfs2_holder gh;
182 for (seg = 0; seg < nr_segs; seg++) {
183 const struct iovec *iv = &iov[seg];
186 * If any segment has a negative length, or the cumulative
187 * length ever wraps negative then return -EINVAL.
189 count += iv->iov_len;
190 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
192 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
197 count -= iv->iov_len; /* This segment is no good */
201 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
202 if (filp->f_flags & O_DIRECT) {
203 loff_t pos = *ppos, size;
204 struct address_space *mapping;
207 mapping = filp->f_mapping;
208 inode = mapping->host;
211 goto out; /* skip atime */
213 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
214 retval = gfs2_glock_nq_m_atime(1, &gh);
217 if (gfs2_is_stuffed(ip)) {
218 gfs2_glock_dq_m(1, &gh);
219 gfs2_holder_uninit(&gh);
220 goto fallback_to_normal;
222 size = i_size_read(inode);
224 retval = gfs2_direct_IO_read(iocb, iov, pos, nr_segs);
225 if (retval > 0 && !is_sync_kiocb(iocb))
226 retval = -EIOCBQUEUED;
228 *ppos = pos + retval;
231 gfs2_glock_dq_m(1, &gh);
232 gfs2_holder_uninit(&gh);
239 for (seg = 0; seg < nr_segs; seg++) {
240 read_descriptor_t desc;
243 desc.arg.buf = iov[seg].iov_base;
244 desc.count = iov[seg].iov_len;
248 do_generic_file_read(filp,ppos,&desc,file_read_actor);
249 retval += desc.written;
251 retval = retval ?: desc.error;
261 * gfs2_read - Read bytes from a file
262 * @file: The file to read from
263 * @buf: The buffer to copy into
264 * @size: The amount of data requested
265 * @offset: The current file offset
267 * Outputs: Offset - updated according to number of bytes read
269 * Returns: The number of bytes read, errno on failure
272 static ssize_t gfs2_read(struct file *filp, char __user *buf, size_t size,
275 struct iovec local_iov = { .iov_base = buf, .iov_len = size };
279 init_sync_kiocb(&kiocb, filp);
280 ret = __gfs2_file_aio_read(&kiocb, &local_iov, 1, offset);
281 if (-EIOCBQUEUED == ret)
282 ret = wait_on_sync_kiocb(&kiocb);
286 static ssize_t gfs2_file_readv(struct file *filp, const struct iovec *iov,
287 unsigned long nr_segs, loff_t *ppos)
292 init_sync_kiocb(&kiocb, filp);
293 ret = __gfs2_file_aio_read(&kiocb, iov, nr_segs, ppos);
294 if (-EIOCBQUEUED == ret)
295 ret = wait_on_sync_kiocb(&kiocb);
299 static ssize_t gfs2_file_aio_read(struct kiocb *iocb, char __user *buf,
300 size_t count, loff_t pos)
302 struct iovec local_iov = { .iov_base = buf, .iov_len = count };
304 BUG_ON(iocb->ki_pos != pos);
305 return __gfs2_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos);
310 * filldir_reg_func - Report a directory entry to the caller of gfs2_dir_read()
311 * @opaque: opaque data used by the function
312 * @name: the name of the directory entry
313 * @length: the length of the name
314 * @offset: the entry's offset in the directory
315 * @inum: the inode number the entry points to
316 * @type: the type of inode the entry points to
318 * Returns: 0 on success, 1 if buffer full
321 static int filldir_reg_func(void *opaque, const char *name, unsigned int length,
322 uint64_t offset, struct gfs2_inum *inum,
325 struct filldir_reg *fdr = (struct filldir_reg *)opaque;
326 struct gfs2_sbd *sdp = fdr->fdr_sbd;
329 error = fdr->fdr_filldir(fdr->fdr_opaque, name, length, offset,
330 inum->no_formal_ino, type);
334 if (fdr->fdr_prefetch && !(length == 1 && *name == '.')) {
335 gfs2_glock_prefetch_num(sdp,
336 inum->no_addr, &gfs2_inode_glops,
337 LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
338 gfs2_glock_prefetch_num(sdp,
339 inum->no_addr, &gfs2_iopen_glops,
340 LM_ST_SHARED, LM_FLAG_TRY);
347 * readdir_reg - Read directory entries from a directory
348 * @file: The directory to read from
349 * @dirent: Buffer for dirents
350 * @filldir: Function used to do the copying
355 static int readdir_reg(struct file *file, void *dirent, filldir_t filldir)
357 struct gfs2_inode *dip = get_v2ip(file->f_mapping->host);
358 struct filldir_reg fdr;
359 struct gfs2_holder d_gh;
360 uint64_t offset = file->f_pos;
363 fdr.fdr_sbd = dip->i_sbd;
364 fdr.fdr_prefetch = 1;
365 fdr.fdr_filldir = filldir;
366 fdr.fdr_opaque = dirent;
368 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
369 error = gfs2_glock_nq_atime(&d_gh);
371 gfs2_holder_uninit(&d_gh);
375 error = gfs2_dir_read(dip, &offset, &fdr, filldir_reg_func);
377 gfs2_glock_dq_uninit(&d_gh);
379 file->f_pos = offset;
385 * filldir_bad_func - Report a directory entry to the caller of gfs2_dir_read()
386 * @opaque: opaque data used by the function
387 * @name: the name of the directory entry
388 * @length: the length of the name
389 * @offset: the entry's offset in the directory
390 * @inum: the inode number the entry points to
391 * @type: the type of inode the entry points to
393 * For supporting NFS.
395 * Returns: 0 on success, 1 if buffer full
398 static int filldir_bad_func(void *opaque, const char *name, unsigned int length,
399 uint64_t offset, struct gfs2_inum *inum,
402 struct filldir_bad *fdb = (struct filldir_bad *)opaque;
403 struct gfs2_sbd *sdp = fdb->fdb_sbd;
404 struct filldir_bad_entry *fbe;
406 if (fdb->fdb_entry_off == fdb->fdb_entry_num ||
407 fdb->fdb_name_off + length > fdb->fdb_name_size)
410 fbe = &fdb->fdb_entry[fdb->fdb_entry_off];
411 fbe->fbe_name = fdb->fdb_name + fdb->fdb_name_off;
412 memcpy(fbe->fbe_name, name, length);
413 fbe->fbe_length = length;
414 fbe->fbe_offset = offset;
415 fbe->fbe_inum = *inum;
416 fbe->fbe_type = type;
418 fdb->fdb_entry_off++;
419 fdb->fdb_name_off += length;
421 if (!(length == 1 && *name == '.')) {
422 gfs2_glock_prefetch_num(sdp,
423 inum->no_addr, &gfs2_inode_glops,
424 LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
425 gfs2_glock_prefetch_num(sdp,
426 inum->no_addr, &gfs2_iopen_glops,
427 LM_ST_SHARED, LM_FLAG_TRY);
434 * readdir_bad - Read directory entries from a directory
435 * @file: The directory to read from
436 * @dirent: Buffer for dirents
437 * @filldir: Function used to do the copying
439 * For supporting NFS.
444 static int readdir_bad(struct file *file, void *dirent, filldir_t filldir)
446 struct gfs2_inode *dip = get_v2ip(file->f_mapping->host);
447 struct gfs2_sbd *sdp = dip->i_sbd;
448 struct filldir_reg fdr;
449 unsigned int entries, size;
450 struct filldir_bad *fdb;
451 struct gfs2_holder d_gh;
452 uint64_t offset = file->f_pos;
454 struct filldir_bad_entry *fbe;
457 entries = gfs2_tune_get(sdp, gt_entries_per_readdir);
458 size = sizeof(struct filldir_bad) +
459 entries * (sizeof(struct filldir_bad_entry) + GFS2_FAST_NAME_SIZE);
461 fdb = kzalloc(size, GFP_KERNEL);
466 fdb->fdb_entry = (struct filldir_bad_entry *)(fdb + 1);
467 fdb->fdb_entry_num = entries;
468 fdb->fdb_name = ((char *)fdb) + sizeof(struct filldir_bad) +
469 entries * sizeof(struct filldir_bad_entry);
470 fdb->fdb_name_size = entries * GFS2_FAST_NAME_SIZE;
472 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
473 error = gfs2_glock_nq_atime(&d_gh);
475 gfs2_holder_uninit(&d_gh);
479 error = gfs2_dir_read(dip, &offset, fdb, filldir_bad_func);
481 gfs2_glock_dq_uninit(&d_gh);
484 fdr.fdr_prefetch = 0;
485 fdr.fdr_filldir = filldir;
486 fdr.fdr_opaque = dirent;
488 for (x = 0; x < fdb->fdb_entry_off; x++) {
489 fbe = &fdb->fdb_entry[x];
491 error = filldir_reg_func(&fdr,
492 fbe->fbe_name, fbe->fbe_length,
494 &fbe->fbe_inum, fbe->fbe_type);
496 file->f_pos = fbe->fbe_offset;
502 file->f_pos = offset;
511 * gfs2_readdir - Read directory entries from a directory
512 * @file: The directory to read from
513 * @dirent: Buffer for dirents
514 * @filldir: Function used to do the copying
519 static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
523 if (strcmp(current->comm, "nfsd") != 0)
524 error = readdir_reg(file, dirent, filldir);
526 error = readdir_bad(file, dirent, filldir);
531 static int gfs2_ioctl_flags(struct gfs2_inode *ip, unsigned int cmd,
534 unsigned int lmode = (cmd == GFS2_IOCTL_SETFLAGS) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
535 struct buffer_head *dibh;
536 struct gfs2_holder i_gh;
538 __u32 flags = 0, change;
540 if (cmd == GFS2_IOCTL_SETFLAGS) {
541 error = get_user(flags, (__u32 __user *)arg);
546 error = gfs2_glock_nq_init(ip->i_gl, lmode, 0, &i_gh);
550 if (cmd == GFS2_IOCTL_SETFLAGS) {
551 change = flags ^ ip->i_di.di_flags;
553 if (change & (GFS2_DIF_IMMUTABLE|GFS2_DIF_APPENDONLY)) {
554 if (!capable(CAP_LINUX_IMMUTABLE))
558 if (flags & (GFS2_DIF_JDATA|GFS2_DIF_DIRECTIO)) {
559 if (!S_ISREG(ip->i_di.di_mode))
562 if (flags & (GFS2_DIF_INHERIT_JDATA|GFS2_DIF_INHERIT_DIRECTIO)) {
563 if (!S_ISDIR(ip->i_di.di_mode))
567 error = gfs2_trans_begin(ip->i_sbd, RES_DINODE, 0);
571 error = gfs2_meta_inode_buffer(ip, &dibh);
575 ip->i_di.di_flags = flags;
577 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
578 gfs2_dinode_out(&ip->i_di, dibh->b_data);
583 gfs2_trans_end(ip->i_sbd);
585 flags = ip->i_di.di_flags;
588 gfs2_glock_dq_uninit(&i_gh);
589 if (cmd == GFS2_IOCTL_GETFLAGS) {
590 if (put_user(flags, (__u32 __user *)arg))
597 * gfs2_ioctl - do an ioctl on a file
599 * @file: the file pointer
600 * @cmd: the ioctl command
606 static int gfs2_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
609 struct gfs2_inode *ip = get_v2ip(inode);
612 case GFS2_IOCTL_SETFLAGS:
613 case GFS2_IOCTL_GETFLAGS:
614 return gfs2_ioctl_flags(ip, cmd, arg);
623 * @file: The file to map
624 * @vma: The VMA which described the mapping
626 * Returns: 0 or error code
629 static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
631 struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
632 struct gfs2_holder i_gh;
635 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
636 error = gfs2_glock_nq_atime(&i_gh);
638 gfs2_holder_uninit(&i_gh);
642 /* This is VM_MAYWRITE instead of VM_WRITE because a call
643 to mprotect() can turn on VM_WRITE later. */
645 if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
646 (VM_MAYSHARE | VM_MAYWRITE))
647 vma->vm_ops = &gfs2_vm_ops_sharewrite;
649 vma->vm_ops = &gfs2_vm_ops_private;
651 gfs2_glock_dq_uninit(&i_gh);
657 * gfs2_open - open a file
658 * @inode: the inode to open
659 * @file: the struct file for this opening
664 static int gfs2_open(struct inode *inode, struct file *file)
666 struct gfs2_inode *ip = get_v2ip(inode);
667 struct gfs2_holder i_gh;
668 struct gfs2_file *fp;
671 fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
675 mutex_init(&fp->f_fl_mutex);
680 gfs2_assert_warn(ip->i_sbd, !get_v2fp(file));
683 if (S_ISREG(ip->i_di.di_mode)) {
684 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
689 if (!(file->f_flags & O_LARGEFILE) &&
690 ip->i_di.di_size > MAX_NON_LFS) {
695 /* Listen to the Direct I/O flag */
697 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
698 file->f_flags |= O_DIRECT;
700 gfs2_glock_dq_uninit(&i_gh);
706 gfs2_glock_dq_uninit(&i_gh);
709 set_v2fp(file, NULL);
716 * gfs2_close - called to close a struct file
717 * @inode: the inode the struct file belongs to
718 * @file: the struct file being closed
723 static int gfs2_close(struct inode *inode, struct file *file)
725 struct gfs2_sbd *sdp = get_v2sdp(inode->i_sb);
726 struct gfs2_file *fp;
729 set_v2fp(file, NULL);
731 if (gfs2_assert_warn(sdp, fp))
740 * gfs2_fsync - sync the dirty data for a file (across the cluster)
741 * @file: the file that points to the dentry (we ignore this)
742 * @dentry: the dentry that points to the inode to sync
747 static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
749 struct gfs2_inode *ip = get_v2ip(dentry->d_inode);
751 gfs2_log_flush_glock(ip->i_gl);
757 * gfs2_lock - acquire/release a posix lock on a file
758 * @file: the file pointer
759 * @cmd: either modify or retrieve lock state, possibly wait
760 * @fl: type and range of lock
765 static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
767 struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
768 struct gfs2_sbd *sdp = ip->i_sbd;
769 struct lm_lockname name =
770 { .ln_number = ip->i_num.no_addr,
771 .ln_type = LM_TYPE_PLOCK };
773 if (!(fl->fl_flags & FL_POSIX))
775 if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
778 if (sdp->sd_args.ar_localflocks) {
780 struct file_lock *tmp;
782 tmp = posix_test_lock(file, fl);
783 fl->fl_type = F_UNLCK;
785 memcpy(fl, tmp, sizeof(struct file_lock));
791 error = posix_lock_file_wait(file, fl);
798 return gfs2_lm_plock_get(sdp, &name, file, fl);
799 else if (fl->fl_type == F_UNLCK)
800 return gfs2_lm_punlock(sdp, &name, file, fl);
802 return gfs2_lm_plock(sdp, &name, file, cmd, fl);
806 * gfs2_sendfile - Send bytes to a file or socket
807 * @in_file: The file to read from
808 * @out_file: The file to write to
809 * @count: The amount of data
810 * @offset: The beginning file offset
812 * Outputs: offset - updated according to number of bytes read
814 * Returns: The number of bytes sent, errno on failure
817 static ssize_t gfs2_sendfile(struct file *in_file, loff_t *offset, size_t count,
818 read_actor_t actor, void *target)
820 return generic_file_sendfile(in_file, offset, count, actor, target);
823 static int do_flock(struct file *file, int cmd, struct file_lock *fl)
825 struct gfs2_file *fp = get_v2fp(file);
826 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
827 struct gfs2_inode *ip = fp->f_inode;
828 struct gfs2_glock *gl;
833 state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
834 flags = ((IS_SETLKW(cmd)) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
836 mutex_lock(&fp->f_fl_mutex);
840 if (fl_gh->gh_state == state)
843 flock_lock_file_wait(file,
844 &(struct file_lock){.fl_type = F_UNLCK});
845 gfs2_glock_dq_uninit(fl_gh);
847 error = gfs2_glock_get(ip->i_sbd,
848 ip->i_num.no_addr, &gfs2_flock_glops,
854 gfs2_holder_init(gl, state, flags, fl_gh);
857 error = gfs2_glock_nq(fl_gh);
859 gfs2_holder_uninit(fl_gh);
860 if (error == GLR_TRYFAILED)
863 error = flock_lock_file_wait(file, fl);
864 gfs2_assert_warn(ip->i_sbd, !error);
868 mutex_unlock(&fp->f_fl_mutex);
873 static void do_unflock(struct file *file, struct file_lock *fl)
875 struct gfs2_file *fp = get_v2fp(file);
876 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
878 mutex_lock(&fp->f_fl_mutex);
879 flock_lock_file_wait(file, fl);
881 gfs2_glock_dq_uninit(fl_gh);
882 mutex_unlock(&fp->f_fl_mutex);
886 * gfs2_flock - acquire/release a flock lock on a file
887 * @file: the file pointer
888 * @cmd: either modify or retrieve lock state, possibly wait
889 * @fl: type and range of lock
894 static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
896 struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
897 struct gfs2_sbd *sdp = ip->i_sbd;
899 if (!(fl->fl_flags & FL_FLOCK))
901 if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
904 if (sdp->sd_args.ar_localflocks)
905 return flock_lock_file_wait(file, fl);
907 if (fl->fl_type == F_UNLCK) {
908 do_unflock(file, fl);
911 return do_flock(file, cmd, fl);
914 struct file_operations gfs2_file_fops = {
915 .llseek = gfs2_llseek,
917 .readv = gfs2_file_readv,
918 .aio_read = gfs2_file_aio_read,
919 .write = generic_file_write,
920 .writev = generic_file_writev,
921 .aio_write = generic_file_aio_write,
925 .release = gfs2_close,
928 .sendfile = gfs2_sendfile,
932 struct file_operations gfs2_dir_fops = {
933 .readdir = gfs2_readdir,
936 .release = gfs2_close,