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) ?
535 LM_ST_EXCLUSIVE : LM_ST_SHARED;
536 struct buffer_head *dibh;
537 struct gfs2_holder i_gh;
539 __u32 flags = 0, change;
541 if (cmd == GFS2_IOCTL_SETFLAGS) {
542 error = get_user(flags, (__u32 __user *)arg);
547 error = gfs2_glock_nq_init(ip->i_gl, lmode, 0, &i_gh);
551 if (cmd == GFS2_IOCTL_SETFLAGS) {
552 change = flags ^ ip->i_di.di_flags;
554 if (change & (GFS2_DIF_IMMUTABLE|GFS2_DIF_APPENDONLY)) {
555 if (!capable(CAP_LINUX_IMMUTABLE))
559 if (flags & (GFS2_DIF_JDATA|GFS2_DIF_DIRECTIO)) {
560 if (!S_ISREG(ip->i_di.di_mode))
564 (GFS2_DIF_INHERIT_JDATA|GFS2_DIF_INHERIT_DIRECTIO)) {
565 if (!S_ISDIR(ip->i_di.di_mode))
569 error = gfs2_trans_begin(ip->i_sbd, RES_DINODE, 0);
573 error = gfs2_meta_inode_buffer(ip, &dibh);
577 ip->i_di.di_flags = flags;
579 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
580 gfs2_dinode_out(&ip->i_di, dibh->b_data);
585 gfs2_trans_end(ip->i_sbd);
587 flags = ip->i_di.di_flags;
590 gfs2_glock_dq_uninit(&i_gh);
591 if (cmd == GFS2_IOCTL_GETFLAGS) {
592 if (put_user(flags, (__u32 __user *)arg))
599 * gfs2_ioctl - do an ioctl on a file
601 * @file: the file pointer
602 * @cmd: the ioctl command
608 static int gfs2_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
611 struct gfs2_inode *ip = get_v2ip(inode);
614 case GFS2_IOCTL_SETFLAGS:
615 case GFS2_IOCTL_GETFLAGS:
616 return gfs2_ioctl_flags(ip, cmd, arg);
625 * @file: The file to map
626 * @vma: The VMA which described the mapping
628 * Returns: 0 or error code
631 static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
633 struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
634 struct gfs2_holder i_gh;
637 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
638 error = gfs2_glock_nq_atime(&i_gh);
640 gfs2_holder_uninit(&i_gh);
644 /* This is VM_MAYWRITE instead of VM_WRITE because a call
645 to mprotect() can turn on VM_WRITE later. */
647 if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
648 (VM_MAYSHARE | VM_MAYWRITE))
649 vma->vm_ops = &gfs2_vm_ops_sharewrite;
651 vma->vm_ops = &gfs2_vm_ops_private;
653 gfs2_glock_dq_uninit(&i_gh);
659 * gfs2_open - open a file
660 * @inode: the inode to open
661 * @file: the struct file for this opening
666 static int gfs2_open(struct inode *inode, struct file *file)
668 struct gfs2_inode *ip = get_v2ip(inode);
669 struct gfs2_holder i_gh;
670 struct gfs2_file *fp;
673 fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
677 mutex_init(&fp->f_fl_mutex);
682 gfs2_assert_warn(ip->i_sbd, !get_v2fp(file));
685 if (S_ISREG(ip->i_di.di_mode)) {
686 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
691 if (!(file->f_flags & O_LARGEFILE) &&
692 ip->i_di.di_size > MAX_NON_LFS) {
697 /* Listen to the Direct I/O flag */
699 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
700 file->f_flags |= O_DIRECT;
702 gfs2_glock_dq_uninit(&i_gh);
708 gfs2_glock_dq_uninit(&i_gh);
711 set_v2fp(file, NULL);
718 * gfs2_close - called to close a struct file
719 * @inode: the inode the struct file belongs to
720 * @file: the struct file being closed
725 static int gfs2_close(struct inode *inode, struct file *file)
727 struct gfs2_sbd *sdp = get_v2sdp(inode->i_sb);
728 struct gfs2_file *fp;
731 set_v2fp(file, NULL);
733 if (gfs2_assert_warn(sdp, fp))
742 * gfs2_fsync - sync the dirty data for a file (across the cluster)
743 * @file: the file that points to the dentry (we ignore this)
744 * @dentry: the dentry that points to the inode to sync
749 static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
751 struct gfs2_inode *ip = get_v2ip(dentry->d_inode);
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 if (!(fl->fl_flags & FL_POSIX))
777 if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
780 if (sdp->sd_args.ar_localflocks) {
782 struct file_lock *tmp;
784 tmp = posix_test_lock(file, fl);
785 fl->fl_type = F_UNLCK;
787 memcpy(fl, tmp, sizeof(struct file_lock));
793 error = posix_lock_file_wait(file, fl);
800 return gfs2_lm_plock_get(sdp, &name, file, fl);
801 else if (fl->fl_type == F_UNLCK)
802 return gfs2_lm_punlock(sdp, &name, file, fl);
804 return gfs2_lm_plock(sdp, &name, file, cmd, fl);
808 * gfs2_sendfile - Send bytes to a file or socket
809 * @in_file: The file to read from
810 * @out_file: The file to write to
811 * @count: The amount of data
812 * @offset: The beginning file offset
814 * Outputs: offset - updated according to number of bytes read
816 * Returns: The number of bytes sent, errno on failure
819 static ssize_t gfs2_sendfile(struct file *in_file, loff_t *offset, size_t count,
820 read_actor_t actor, void *target)
822 return generic_file_sendfile(in_file, offset, count, actor, target);
825 static int do_flock(struct file *file, int cmd, struct file_lock *fl)
827 struct gfs2_file *fp = get_v2fp(file);
828 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
829 struct gfs2_inode *ip = fp->f_inode;
830 struct gfs2_glock *gl;
835 state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
836 flags = ((IS_SETLKW(cmd)) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
838 mutex_lock(&fp->f_fl_mutex);
842 if (fl_gh->gh_state == state)
845 flock_lock_file_wait(file,
846 &(struct file_lock){.fl_type = F_UNLCK});
847 gfs2_glock_dq_uninit(fl_gh);
849 error = gfs2_glock_get(ip->i_sbd,
850 ip->i_num.no_addr, &gfs2_flock_glops,
856 gfs2_holder_init(gl, state, flags, fl_gh);
859 error = gfs2_glock_nq(fl_gh);
861 gfs2_holder_uninit(fl_gh);
862 if (error == GLR_TRYFAILED)
865 error = flock_lock_file_wait(file, fl);
866 gfs2_assert_warn(ip->i_sbd, !error);
870 mutex_unlock(&fp->f_fl_mutex);
875 static void do_unflock(struct file *file, struct file_lock *fl)
877 struct gfs2_file *fp = get_v2fp(file);
878 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
880 mutex_lock(&fp->f_fl_mutex);
881 flock_lock_file_wait(file, fl);
883 gfs2_glock_dq_uninit(fl_gh);
884 mutex_unlock(&fp->f_fl_mutex);
888 * gfs2_flock - acquire/release a flock lock on a file
889 * @file: the file pointer
890 * @cmd: either modify or retrieve lock state, possibly wait
891 * @fl: type and range of lock
896 static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
898 struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
899 struct gfs2_sbd *sdp = ip->i_sbd;
901 if (!(fl->fl_flags & FL_FLOCK))
903 if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
906 if (sdp->sd_args.ar_localflocks)
907 return flock_lock_file_wait(file, fl);
909 if (fl->fl_type == F_UNLCK) {
910 do_unflock(file, fl);
913 return do_flock(file, cmd, fl);
916 struct file_operations gfs2_file_fops = {
917 .llseek = gfs2_llseek,
919 .readv = gfs2_file_readv,
920 .aio_read = gfs2_file_aio_read,
921 .write = generic_file_write,
922 .writev = generic_file_writev,
923 .aio_write = generic_file_aio_write,
927 .release = gfs2_close,
930 .sendfile = gfs2_sendfile,
934 struct file_operations gfs2_dir_fops = {
935 .readdir = gfs2_readdir,
938 .release = gfs2_close,