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;
70 static int gfs2_read_actor(read_descriptor_t *desc, struct page *page,
71 unsigned long offset, unsigned long size)
74 unsigned long count = desc->count;
80 memcpy(desc->arg.buf, kaddr + offset, size);
83 desc->count = count - size;
84 desc->written += size;
85 desc->arg.buf += size;
89 int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
90 char *buf, loff_t *pos, unsigned size)
92 struct inode *inode = ip->i_vnode;
93 read_descriptor_t desc;
98 do_generic_mapping_read(inode->i_mapping, ra_state, NULL, pos, &desc, gfs2_read_actor);
99 return desc.written ? desc.written : desc.error;
103 * gfs2_llseek - seek to a location in a file
105 * @offset: the offset
106 * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
108 * SEEK_END requires the glock for the file because it references the
111 * Returns: The new offset, or errno
114 static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
116 struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
117 struct gfs2_holder i_gh;
120 atomic_inc(&ip->i_sbd->sd_ops_file);
123 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
126 error = remote_llseek(file, offset, origin);
127 gfs2_glock_dq_uninit(&i_gh);
130 error = remote_llseek(file, offset, origin);
136 static ssize_t gfs2_direct_IO_read(struct kiocb *iocb, const struct iovec *iov,
137 loff_t offset, unsigned long nr_segs)
139 struct file *file = iocb->ki_filp;
140 struct address_space *mapping = file->f_mapping;
143 retval = filemap_write_and_wait(mapping);
145 retval = mapping->a_ops->direct_IO(READ, iocb, iov, offset,
152 * __gfs2_file_aio_read - The main GFS2 read function
154 * N.B. This is almost, but not quite the same as __generic_file_aio_read()
155 * the important subtle different being that inode->i_size isn't valid
156 * unless we are holding a lock, and we do this _only_ on the O_DIRECT
157 * path since otherwise locking is done entirely at the page cache
160 static ssize_t __gfs2_file_aio_read(struct kiocb *iocb,
161 const struct iovec *iov,
162 unsigned long nr_segs, loff_t *ppos)
164 struct file *filp = iocb->ki_filp;
165 struct gfs2_inode *ip = get_v2ip(filp->f_mapping->host);
166 struct gfs2_holder gh;
172 for (seg = 0; seg < nr_segs; seg++) {
173 const struct iovec *iv = &iov[seg];
176 * If any segment has a negative length, or the cumulative
177 * length ever wraps negative then return -EINVAL.
179 count += iv->iov_len;
180 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
182 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
187 count -= iv->iov_len; /* This segment is no good */
191 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
192 if (filp->f_flags & O_DIRECT) {
193 loff_t pos = *ppos, size;
194 struct address_space *mapping;
197 mapping = filp->f_mapping;
198 inode = mapping->host;
201 goto out; /* skip atime */
203 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
204 retval = gfs2_glock_nq_m_atime(1, &gh);
208 size = i_size_read(inode);
210 retval = gfs2_direct_IO_read(iocb, iov, pos, nr_segs);
211 if (retval > 0 && !is_sync_kiocb(iocb))
212 retval = -EIOCBQUEUED;
214 *ppos = pos + retval;
217 gfs2_glock_dq_m(1, &gh);
218 gfs2_holder_uninit(&gh);
224 for (seg = 0; seg < nr_segs; seg++) {
225 read_descriptor_t desc;
228 desc.arg.buf = iov[seg].iov_base;
229 desc.count = iov[seg].iov_len;
233 do_generic_file_read(filp,ppos,&desc,file_read_actor);
234 retval += desc.written;
236 retval = retval ?: desc.error;
246 * gfs2_read - Read bytes from a file
247 * @file: The file to read from
248 * @buf: The buffer to copy into
249 * @size: The amount of data requested
250 * @offset: The current file offset
252 * Outputs: Offset - updated according to number of bytes read
254 * Returns: The number of bytes read, errno on failure
257 static ssize_t gfs2_read(struct file *filp, char __user *buf, size_t size,
260 struct iovec local_iov = { .iov_base = buf, .iov_len = size };
264 atomic_inc(&get_v2sdp(filp->f_mapping->host->i_sb)->sd_ops_file);
266 init_sync_kiocb(&kiocb, filp);
267 ret = __gfs2_file_aio_read(&kiocb, &local_iov, 1, offset);
268 if (-EIOCBQUEUED == ret)
269 ret = wait_on_sync_kiocb(&kiocb);
273 static ssize_t gfs2_file_readv(struct file *filp, const struct iovec *iov,
274 unsigned long nr_segs, loff_t *ppos)
279 atomic_inc(&get_v2sdp(filp->f_mapping->host->i_sb)->sd_ops_file);
281 init_sync_kiocb(&kiocb, filp);
282 ret = __gfs2_file_aio_read(&kiocb, iov, nr_segs, ppos);
283 if (-EIOCBQUEUED == ret)
284 ret = wait_on_sync_kiocb(&kiocb);
288 static ssize_t gfs2_file_aio_read(struct kiocb *iocb, char __user *buf,
289 size_t count, loff_t pos)
291 struct file *filp = iocb->ki_filp;
292 struct iovec local_iov = { .iov_base = buf, .iov_len = count };
294 atomic_inc(&get_v2sdp(filp->f_mapping->host->i_sb)->sd_ops_file);
296 BUG_ON(iocb->ki_pos != pos);
297 return __gfs2_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos);
302 * filldir_reg_func - Report a directory entry to the caller of gfs2_dir_read()
303 * @opaque: opaque data used by the function
304 * @name: the name of the directory entry
305 * @length: the length of the name
306 * @offset: the entry's offset in the directory
307 * @inum: the inode number the entry points to
308 * @type: the type of inode the entry points to
310 * Returns: 0 on success, 1 if buffer full
313 static int filldir_reg_func(void *opaque, const char *name, unsigned int length,
314 uint64_t offset, struct gfs2_inum *inum,
317 struct filldir_reg *fdr = (struct filldir_reg *)opaque;
318 struct gfs2_sbd *sdp = fdr->fdr_sbd;
321 error = fdr->fdr_filldir(fdr->fdr_opaque, name, length, offset,
322 inum->no_formal_ino, type);
326 if (fdr->fdr_prefetch && !(length == 1 && *name == '.')) {
327 gfs2_glock_prefetch_num(sdp,
328 inum->no_addr, &gfs2_inode_glops,
329 LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
330 gfs2_glock_prefetch_num(sdp,
331 inum->no_addr, &gfs2_iopen_glops,
332 LM_ST_SHARED, LM_FLAG_TRY);
339 * readdir_reg - Read directory entries from a directory
340 * @file: The directory to read from
341 * @dirent: Buffer for dirents
342 * @filldir: Function used to do the copying
347 static int readdir_reg(struct file *file, void *dirent, filldir_t filldir)
349 struct gfs2_inode *dip = get_v2ip(file->f_mapping->host);
350 struct filldir_reg fdr;
351 struct gfs2_holder d_gh;
352 uint64_t offset = file->f_pos;
355 fdr.fdr_sbd = dip->i_sbd;
356 fdr.fdr_prefetch = 1;
357 fdr.fdr_filldir = filldir;
358 fdr.fdr_opaque = dirent;
360 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
361 error = gfs2_glock_nq_atime(&d_gh);
363 gfs2_holder_uninit(&d_gh);
367 error = gfs2_dir_read(dip, &offset, &fdr, filldir_reg_func);
369 gfs2_glock_dq_uninit(&d_gh);
371 file->f_pos = offset;
377 * filldir_bad_func - Report a directory entry to the caller of gfs2_dir_read()
378 * @opaque: opaque data used by the function
379 * @name: the name of the directory entry
380 * @length: the length of the name
381 * @offset: the entry's offset in the directory
382 * @inum: the inode number the entry points to
383 * @type: the type of inode the entry points to
385 * For supporting NFS.
387 * Returns: 0 on success, 1 if buffer full
390 static int filldir_bad_func(void *opaque, const char *name, unsigned int length,
391 uint64_t offset, struct gfs2_inum *inum,
394 struct filldir_bad *fdb = (struct filldir_bad *)opaque;
395 struct gfs2_sbd *sdp = fdb->fdb_sbd;
396 struct filldir_bad_entry *fbe;
398 if (fdb->fdb_entry_off == fdb->fdb_entry_num ||
399 fdb->fdb_name_off + length > fdb->fdb_name_size)
402 fbe = &fdb->fdb_entry[fdb->fdb_entry_off];
403 fbe->fbe_name = fdb->fdb_name + fdb->fdb_name_off;
404 memcpy(fbe->fbe_name, name, length);
405 fbe->fbe_length = length;
406 fbe->fbe_offset = offset;
407 fbe->fbe_inum = *inum;
408 fbe->fbe_type = type;
410 fdb->fdb_entry_off++;
411 fdb->fdb_name_off += length;
413 if (!(length == 1 && *name == '.')) {
414 gfs2_glock_prefetch_num(sdp,
415 inum->no_addr, &gfs2_inode_glops,
416 LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
417 gfs2_glock_prefetch_num(sdp,
418 inum->no_addr, &gfs2_iopen_glops,
419 LM_ST_SHARED, LM_FLAG_TRY);
426 * readdir_bad - Read directory entries from a directory
427 * @file: The directory to read from
428 * @dirent: Buffer for dirents
429 * @filldir: Function used to do the copying
431 * For supporting NFS.
436 static int readdir_bad(struct file *file, void *dirent, filldir_t filldir)
438 struct gfs2_inode *dip = get_v2ip(file->f_mapping->host);
439 struct gfs2_sbd *sdp = dip->i_sbd;
440 struct filldir_reg fdr;
441 unsigned int entries, size;
442 struct filldir_bad *fdb;
443 struct gfs2_holder d_gh;
444 uint64_t offset = file->f_pos;
446 struct filldir_bad_entry *fbe;
449 entries = gfs2_tune_get(sdp, gt_entries_per_readdir);
450 size = sizeof(struct filldir_bad) +
451 entries * (sizeof(struct filldir_bad_entry) + GFS2_FAST_NAME_SIZE);
453 fdb = kzalloc(size, GFP_KERNEL);
458 fdb->fdb_entry = (struct filldir_bad_entry *)(fdb + 1);
459 fdb->fdb_entry_num = entries;
460 fdb->fdb_name = ((char *)fdb) + sizeof(struct filldir_bad) +
461 entries * sizeof(struct filldir_bad_entry);
462 fdb->fdb_name_size = entries * GFS2_FAST_NAME_SIZE;
464 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
465 error = gfs2_glock_nq_atime(&d_gh);
467 gfs2_holder_uninit(&d_gh);
471 error = gfs2_dir_read(dip, &offset, fdb, filldir_bad_func);
473 gfs2_glock_dq_uninit(&d_gh);
476 fdr.fdr_prefetch = 0;
477 fdr.fdr_filldir = filldir;
478 fdr.fdr_opaque = dirent;
480 for (x = 0; x < fdb->fdb_entry_off; x++) {
481 fbe = &fdb->fdb_entry[x];
483 error = filldir_reg_func(&fdr,
484 fbe->fbe_name, fbe->fbe_length,
486 &fbe->fbe_inum, fbe->fbe_type);
488 file->f_pos = fbe->fbe_offset;
494 file->f_pos = offset;
503 * gfs2_readdir - Read directory entries from a directory
504 * @file: The directory to read from
505 * @dirent: Buffer for dirents
506 * @filldir: Function used to do the copying
511 static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
515 atomic_inc(&get_v2sdp(file->f_mapping->host->i_sb)->sd_ops_file);
517 if (strcmp(current->comm, "nfsd") != 0)
518 error = readdir_reg(file, dirent, filldir);
520 error = readdir_bad(file, dirent, filldir);
525 static int gfs2_ioctl_flags(struct gfs2_inode *ip, unsigned int cmd, unsigned long arg)
527 unsigned int lmode = (cmd == GFS2_IOCTL_SETFLAGS) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
528 struct buffer_head *dibh;
529 struct gfs2_holder i_gh;
531 __u32 flags = 0, change;
533 if (cmd == GFS2_IOCTL_SETFLAGS) {
534 error = get_user(flags, (__u32 __user *)arg);
539 error = gfs2_glock_nq_init(ip->i_gl, lmode, 0, &i_gh);
543 if (cmd == GFS2_IOCTL_SETFLAGS) {
544 change = flags ^ ip->i_di.di_flags;
546 if (change & (GFS2_DIF_IMMUTABLE|GFS2_DIF_APPENDONLY)) {
547 if (!capable(CAP_LINUX_IMMUTABLE))
551 if (flags & (GFS2_DIF_JDATA|GFS2_DIF_DIRECTIO)) {
552 if (!S_ISREG(ip->i_di.di_mode))
555 if (flags & (GFS2_DIF_INHERIT_JDATA|GFS2_DIF_INHERIT_DIRECTIO)) {
556 if (!S_ISDIR(ip->i_di.di_mode))
560 error = gfs2_trans_begin(ip->i_sbd, RES_DINODE, 0);
564 error = gfs2_meta_inode_buffer(ip, &dibh);
568 ip->i_di.di_flags = flags;
570 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
571 gfs2_dinode_out(&ip->i_di, dibh->b_data);
576 gfs2_trans_end(ip->i_sbd);
578 flags = ip->i_di.di_flags;
581 gfs2_glock_dq_uninit(&i_gh);
582 if (cmd == GFS2_IOCTL_GETFLAGS) {
583 if (put_user(flags, (__u32 __user *)arg))
590 * gfs2_ioctl - do an ioctl on a file
592 * @file: the file pointer
593 * @cmd: the ioctl command
599 static int gfs2_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
602 struct gfs2_inode *ip = get_v2ip(inode);
604 atomic_inc(&ip->i_sbd->sd_ops_file);
607 case GFS2_IOCTL_SETFLAGS:
608 case GFS2_IOCTL_GETFLAGS:
609 return gfs2_ioctl_flags(ip, cmd, arg);
618 * @file: The file to map
619 * @vma: The VMA which described the mapping
621 * Returns: 0 or error code
624 static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
626 struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
627 struct gfs2_holder i_gh;
630 atomic_inc(&ip->i_sbd->sd_ops_file);
632 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
633 error = gfs2_glock_nq_atime(&i_gh);
635 gfs2_holder_uninit(&i_gh);
639 /* This is VM_MAYWRITE instead of VM_WRITE because a call
640 to mprotect() can turn on VM_WRITE later. */
642 if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
643 (VM_MAYSHARE | VM_MAYWRITE))
644 vma->vm_ops = &gfs2_vm_ops_sharewrite;
646 vma->vm_ops = &gfs2_vm_ops_private;
648 gfs2_glock_dq_uninit(&i_gh);
654 * gfs2_open - open a file
655 * @inode: the inode to open
656 * @file: the struct file for this opening
661 static int gfs2_open(struct inode *inode, struct file *file)
663 struct gfs2_inode *ip = get_v2ip(inode);
664 struct gfs2_holder i_gh;
665 struct gfs2_file *fp;
668 atomic_inc(&ip->i_sbd->sd_ops_file);
670 fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
674 init_MUTEX(&fp->f_fl_mutex);
679 gfs2_assert_warn(ip->i_sbd, !get_v2fp(file));
682 if (S_ISREG(ip->i_di.di_mode)) {
683 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
688 if (!(file->f_flags & O_LARGEFILE) &&
689 ip->i_di.di_size > MAX_NON_LFS) {
694 /* Listen to the Direct I/O flag */
696 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
697 file->f_flags |= O_DIRECT;
699 gfs2_glock_dq_uninit(&i_gh);
705 gfs2_glock_dq_uninit(&i_gh);
708 set_v2fp(file, NULL);
715 * gfs2_close - called to close a struct file
716 * @inode: the inode the struct file belongs to
717 * @file: the struct file being closed
722 static int gfs2_close(struct inode *inode, struct file *file)
724 struct gfs2_sbd *sdp = get_v2sdp(inode->i_sb);
725 struct gfs2_file *fp;
727 atomic_inc(&sdp->sd_ops_file);
730 set_v2fp(file, NULL);
732 if (gfs2_assert_warn(sdp, fp))
741 * gfs2_fsync - sync the dirty data for a file (across the cluster)
742 * @file: the file that points to the dentry (we ignore this)
743 * @dentry: the dentry that points to the inode to sync
748 static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
750 struct gfs2_inode *ip = get_v2ip(dentry->d_inode);
752 atomic_inc(&ip->i_sbd->sd_ops_file);
753 gfs2_log_flush_glock(ip->i_gl);
759 * gfs2_lock - acquire/release a posix lock on a file
760 * @file: the file pointer
761 * @cmd: either modify or retrieve lock state, possibly wait
762 * @fl: type and range of lock
767 static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
769 struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
770 struct gfs2_sbd *sdp = ip->i_sbd;
771 struct lm_lockname name =
772 { .ln_number = ip->i_num.no_addr,
773 .ln_type = LM_TYPE_PLOCK };
775 atomic_inc(&sdp->sd_ops_file);
777 if (!(fl->fl_flags & FL_POSIX))
779 if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
782 if (sdp->sd_args.ar_localflocks) {
784 struct file_lock *tmp;
786 tmp = posix_test_lock(file, fl);
787 fl->fl_type = F_UNLCK;
789 memcpy(fl, tmp, sizeof(struct file_lock));
795 error = posix_lock_file_wait(file, fl);
802 return gfs2_lm_plock_get(sdp, &name, file, fl);
803 else if (fl->fl_type == F_UNLCK)
804 return gfs2_lm_punlock(sdp, &name, file, fl);
806 return gfs2_lm_plock(sdp, &name, file, cmd, fl);
810 * gfs2_sendfile - Send bytes to a file or socket
811 * @in_file: The file to read from
812 * @out_file: The file to write to
813 * @count: The amount of data
814 * @offset: The beginning file offset
816 * Outputs: offset - updated according to number of bytes read
818 * Returns: The number of bytes sent, errno on failure
821 static ssize_t gfs2_sendfile(struct file *in_file, loff_t *offset, size_t count,
822 read_actor_t actor, void *target)
824 struct gfs2_inode *ip = get_v2ip(in_file->f_mapping->host);
826 atomic_inc(&ip->i_sbd->sd_ops_file);
828 return generic_file_sendfile(in_file, offset, count, actor, target);
831 static int do_flock(struct file *file, int cmd, struct file_lock *fl)
833 struct gfs2_file *fp = get_v2fp(file);
834 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
835 struct gfs2_inode *ip = fp->f_inode;
836 struct gfs2_glock *gl;
841 state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
842 flags = ((IS_SETLKW(cmd)) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
844 down(&fp->f_fl_mutex);
848 if (fl_gh->gh_state == state)
851 flock_lock_file_wait(file,
852 &(struct file_lock){.fl_type = F_UNLCK});
853 gfs2_glock_dq_uninit(fl_gh);
855 error = gfs2_glock_get(ip->i_sbd,
856 ip->i_num.no_addr, &gfs2_flock_glops,
862 gfs2_holder_init(gl, state, flags, fl_gh);
865 error = gfs2_glock_nq(fl_gh);
867 gfs2_holder_uninit(fl_gh);
868 if (error == GLR_TRYFAILED)
871 error = flock_lock_file_wait(file, fl);
872 gfs2_assert_warn(ip->i_sbd, !error);
881 static void do_unflock(struct file *file, struct file_lock *fl)
883 struct gfs2_file *fp = get_v2fp(file);
884 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
886 down(&fp->f_fl_mutex);
887 flock_lock_file_wait(file, fl);
889 gfs2_glock_dq_uninit(fl_gh);
894 * gfs2_flock - acquire/release a flock lock on a file
895 * @file: the file pointer
896 * @cmd: either modify or retrieve lock state, possibly wait
897 * @fl: type and range of lock
902 static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
904 struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
905 struct gfs2_sbd *sdp = ip->i_sbd;
907 atomic_inc(&ip->i_sbd->sd_ops_file);
909 if (!(fl->fl_flags & FL_FLOCK))
911 if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
914 if (sdp->sd_args.ar_localflocks)
915 return flock_lock_file_wait(file, fl);
917 if (fl->fl_type == F_UNLCK) {
918 do_unflock(file, fl);
921 return do_flock(file, cmd, fl);
924 struct file_operations gfs2_file_fops = {
925 .llseek = gfs2_llseek,
927 .readv = gfs2_file_readv,
928 .aio_read = gfs2_file_aio_read,
929 .write = generic_file_write,
930 .writev = generic_file_writev,
931 .aio_write = generic_file_aio_write,
935 .release = gfs2_close,
938 .sendfile = gfs2_sendfile,
942 struct file_operations gfs2_dir_fops = {
943 .readdir = gfs2_readdir,
946 .release = gfs2_close,