[GFS2] Make journaled data files identical to normal files on disk
[safe/jmp/linux-2.6] / fs / gfs2 / ops_file.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2005 Red Hat, Inc.  All rights reserved.
4  *
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.
8  */
9
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>
18 #include <linux/mm.h>
19 #include <linux/smp_lock.h>
20 #include <linux/gfs2_ioctl.h>
21 #include <linux/fs.h>
22 #include <asm/semaphore.h>
23 #include <asm/uaccess.h>
24
25 #include "gfs2.h"
26 #include "bmap.h"
27 #include "dir.h"
28 #include "glock.h"
29 #include "glops.h"
30 #include "inode.h"
31 #include "lm.h"
32 #include "log.h"
33 #include "meta_io.h"
34 #include "ops_file.h"
35 #include "ops_vm.h"
36 #include "quota.h"
37 #include "rgrp.h"
38 #include "trans.h"
39
40 /* "bad" is for NFS support */
41 struct filldir_bad_entry {
42         char *fbe_name;
43         unsigned int fbe_length;
44         uint64_t fbe_offset;
45         struct gfs2_inum fbe_inum;
46         unsigned int fbe_type;
47 };
48
49 struct filldir_bad {
50         struct gfs2_sbd *fdb_sbd;
51
52         struct filldir_bad_entry *fdb_entry;
53         unsigned int fdb_entry_num;
54         unsigned int fdb_entry_off;
55
56         char *fdb_name;
57         unsigned int fdb_name_size;
58         unsigned int fdb_name_off;
59 };
60
61 /* For regular, non-NFS */
62 struct filldir_reg {
63         struct gfs2_sbd *fdr_sbd;
64         int fdr_prefetch;
65
66         filldir_t fdr_filldir;
67         void *fdr_opaque;
68 };
69
70 static int gfs2_read_actor(read_descriptor_t *desc, struct page *page,
71                            unsigned long offset, unsigned long size)
72 {
73         char *kaddr;
74         unsigned long count = desc->count;
75
76         if (size > count)
77                 size = count;
78
79         kaddr = kmap(page);
80         memcpy(desc->arg.buf, kaddr + offset, size);
81         kunmap(page);
82
83         desc->count = count - size;
84         desc->written += size;
85         desc->arg.buf += size;
86         return size;
87 }
88
89 int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
90                        char *buf, loff_t *pos, unsigned size)
91 {
92         struct inode *inode = ip->i_vnode;
93         read_descriptor_t desc;
94         desc.written = 0;
95         desc.arg.buf = buf;
96         desc.count = size;
97         desc.error = 0;
98         do_generic_mapping_read(inode->i_mapping, ra_state, NULL, pos, &desc, gfs2_read_actor);
99         return desc.written ? desc.written : desc.error;
100 }
101
102 /**
103  * gfs2_llseek - seek to a location in a file
104  * @file: the file
105  * @offset: the offset
106  * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
107  *
108  * SEEK_END requires the glock for the file because it references the
109  * file's size.
110  *
111  * Returns: The new offset, or errno
112  */
113
114 static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
115 {
116         struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
117         struct gfs2_holder i_gh;
118         loff_t error;
119
120         atomic_inc(&ip->i_sbd->sd_ops_file);
121
122         if (origin == 2) {
123                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
124                                            &i_gh);
125                 if (!error) {
126                         error = remote_llseek(file, offset, origin);
127                         gfs2_glock_dq_uninit(&i_gh);
128                 }
129         } else
130                 error = remote_llseek(file, offset, origin);
131
132         return error;
133 }
134
135
136 static ssize_t gfs2_direct_IO_read(struct kiocb *iocb, const struct iovec *iov,
137                                    loff_t offset, unsigned long nr_segs)
138 {
139         struct file *file = iocb->ki_filp;
140         struct address_space *mapping = file->f_mapping;
141         ssize_t retval;
142
143         retval = filemap_write_and_wait(mapping);
144         if (retval == 0) {
145                 retval = mapping->a_ops->direct_IO(READ, iocb, iov, offset,
146                                                    nr_segs);
147         }
148         return retval;
149 }
150
151 /**
152  * __gfs2_file_aio_read - The main GFS2 read function
153  * 
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
158  * layer.
159  */
160 static ssize_t __gfs2_file_aio_read(struct kiocb *iocb,
161                                     const struct iovec *iov,
162                                     unsigned long nr_segs, loff_t *ppos)
163 {
164         struct file *filp = iocb->ki_filp;
165         struct gfs2_inode *ip = get_v2ip(filp->f_mapping->host);
166         struct gfs2_holder gh;
167         ssize_t retval;
168         unsigned long seg;
169         size_t count;
170
171         count = 0;
172         for (seg = 0; seg < nr_segs; seg++) {
173                 const struct iovec *iv = &iov[seg];
174
175                 /*
176                  * If any segment has a negative length, or the cumulative
177                  * length ever wraps negative then return -EINVAL.
178                  */
179         count += iv->iov_len;
180         if (unlikely((ssize_t)(count|iv->iov_len) < 0))
181                 return -EINVAL;
182         if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
183                 continue;
184         if (seg == 0)
185                 return -EFAULT;
186         nr_segs = seg;
187         count -= iv->iov_len;   /* This segment is no good */
188         break;
189         }
190
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;
195                 struct inode *inode;
196
197                 mapping = filp->f_mapping;
198                 inode = mapping->host;
199                 retval = 0;
200                 if (!count)
201                         goto out; /* skip atime */
202
203                 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
204                 retval = gfs2_glock_nq_m_atime(1, &gh);
205                 if (retval)
206                         goto out;
207
208                 size = i_size_read(inode);
209                 if (pos < size) {
210                          retval = gfs2_direct_IO_read(iocb, iov, pos, nr_segs);
211                         if (retval > 0 && !is_sync_kiocb(iocb))
212                                 retval = -EIOCBQUEUED;
213                         if (retval > 0)
214                                 *ppos = pos + retval;
215                 }
216                 file_accessed(filp);
217                 gfs2_glock_dq_m(1, &gh);
218                 gfs2_holder_uninit(&gh);
219                 goto out;
220         }
221
222         retval = 0;
223         if (count) {
224                 for (seg = 0; seg < nr_segs; seg++) {
225                         read_descriptor_t desc;
226
227                         desc.written = 0;
228                         desc.arg.buf = iov[seg].iov_base;
229                         desc.count = iov[seg].iov_len;
230                         if (desc.count == 0)
231                                 continue;
232                         desc.error = 0;
233                         do_generic_file_read(filp,ppos,&desc,file_read_actor);
234                         retval += desc.written;
235                         if (desc.error) {
236                                 retval = retval ?: desc.error;
237                                  break;
238                         }
239                 }
240         }
241 out:
242         return retval;
243 }
244
245 /**
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
251  *
252  * Outputs: Offset - updated according to number of bytes read
253  *
254  * Returns: The number of bytes read, errno on failure
255  */
256
257 static ssize_t gfs2_read(struct file *filp, char __user *buf, size_t size,
258                          loff_t *offset)
259 {
260         struct iovec local_iov = { .iov_base = buf, .iov_len = size };
261         struct kiocb kiocb;
262         ssize_t ret;
263
264         atomic_inc(&get_v2sdp(filp->f_mapping->host->i_sb)->sd_ops_file);
265
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);
270         return ret;
271 }
272
273 static ssize_t gfs2_file_readv(struct file *filp, const struct iovec *iov,
274                                unsigned long nr_segs, loff_t *ppos)
275 {
276         struct kiocb kiocb;
277         ssize_t ret;
278
279         atomic_inc(&get_v2sdp(filp->f_mapping->host->i_sb)->sd_ops_file);
280
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);
285         return ret;
286 }
287
288 static ssize_t gfs2_file_aio_read(struct kiocb *iocb, char __user *buf,
289                                   size_t count, loff_t pos)
290 {
291         struct file *filp = iocb->ki_filp;
292         struct iovec local_iov = { .iov_base = buf, .iov_len = count };
293
294         atomic_inc(&get_v2sdp(filp->f_mapping->host->i_sb)->sd_ops_file);
295
296         BUG_ON(iocb->ki_pos != pos);
297         return __gfs2_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos);
298 }
299
300
301 /**
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
309  *
310  * Returns: 0 on success, 1 if buffer full
311  */
312
313 static int filldir_reg_func(void *opaque, const char *name, unsigned int length,
314                             uint64_t offset, struct gfs2_inum *inum,
315                             unsigned int type)
316 {
317         struct filldir_reg *fdr = (struct filldir_reg *)opaque;
318         struct gfs2_sbd *sdp = fdr->fdr_sbd;
319         int error;
320
321         error = fdr->fdr_filldir(fdr->fdr_opaque, name, length, offset,
322                                  inum->no_formal_ino, type);
323         if (error)
324                 return 1;
325
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);
333         }
334
335         return 0;
336 }
337
338 /**
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
343  *
344  * Returns: errno
345  */
346
347 static int readdir_reg(struct file *file, void *dirent, filldir_t filldir)
348 {
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;
353         int error;
354
355         fdr.fdr_sbd = dip->i_sbd;
356         fdr.fdr_prefetch = 1;
357         fdr.fdr_filldir = filldir;
358         fdr.fdr_opaque = dirent;
359
360         gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
361         error = gfs2_glock_nq_atime(&d_gh);
362         if (error) {
363                 gfs2_holder_uninit(&d_gh);
364                 return error;
365         }
366
367         error = gfs2_dir_read(dip, &offset, &fdr, filldir_reg_func);
368
369         gfs2_glock_dq_uninit(&d_gh);
370
371         file->f_pos = offset;
372
373         return error;
374 }
375
376 /**
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
384  *
385  * For supporting NFS.
386  *
387  * Returns: 0 on success, 1 if buffer full
388  */
389
390 static int filldir_bad_func(void *opaque, const char *name, unsigned int length,
391                             uint64_t offset, struct gfs2_inum *inum,
392                             unsigned int type)
393 {
394         struct filldir_bad *fdb = (struct filldir_bad *)opaque;
395         struct gfs2_sbd *sdp = fdb->fdb_sbd;
396         struct filldir_bad_entry *fbe;
397
398         if (fdb->fdb_entry_off == fdb->fdb_entry_num ||
399             fdb->fdb_name_off + length > fdb->fdb_name_size)
400                 return 1;
401
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;
409
410         fdb->fdb_entry_off++;
411         fdb->fdb_name_off += length;
412
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);
420         }
421
422         return 0;
423 }
424
425 /**
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
430  *
431  * For supporting NFS.
432  *
433  * Returns: errno
434  */
435
436 static int readdir_bad(struct file *file, void *dirent, filldir_t filldir)
437 {
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;
445         unsigned int x;
446         struct filldir_bad_entry *fbe;
447         int error;
448
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);
452
453         fdb = kzalloc(size, GFP_KERNEL);
454         if (!fdb)
455                 return -ENOMEM;
456
457         fdb->fdb_sbd = sdp;
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;
463
464         gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
465         error = gfs2_glock_nq_atime(&d_gh);
466         if (error) {
467                 gfs2_holder_uninit(&d_gh);
468                 goto out;
469         }
470
471         error = gfs2_dir_read(dip, &offset, fdb, filldir_bad_func);
472
473         gfs2_glock_dq_uninit(&d_gh);
474
475         fdr.fdr_sbd = sdp;
476         fdr.fdr_prefetch = 0;
477         fdr.fdr_filldir = filldir;
478         fdr.fdr_opaque = dirent;
479
480         for (x = 0; x < fdb->fdb_entry_off; x++) {
481                 fbe = &fdb->fdb_entry[x];
482
483                 error = filldir_reg_func(&fdr,
484                                          fbe->fbe_name, fbe->fbe_length,
485                                          fbe->fbe_offset,
486                                          &fbe->fbe_inum, fbe->fbe_type);
487                 if (error) {
488                         file->f_pos = fbe->fbe_offset;
489                         error = 0;
490                         goto out;
491                 }
492         }
493
494         file->f_pos = offset;
495
496  out:
497         kfree(fdb);
498
499         return error;
500 }
501
502 /**
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
507  *
508  * Returns: errno
509  */
510
511 static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
512 {
513         int error;
514
515         atomic_inc(&get_v2sdp(file->f_mapping->host->i_sb)->sd_ops_file);
516
517         if (strcmp(current->comm, "nfsd") != 0)
518                 error = readdir_reg(file, dirent, filldir);
519         else
520                 error = readdir_bad(file, dirent, filldir);
521
522         return error;
523 }
524
525 static int gfs2_ioctl_flags(struct gfs2_inode *ip, unsigned int cmd, unsigned long arg)
526 {
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;
530         int error;
531         __u32 flags = 0, change;
532
533         if (cmd == GFS2_IOCTL_SETFLAGS) {
534                 error = get_user(flags, (__u32 __user *)arg);
535                 if (error)
536                         return -EFAULT;
537         }
538
539         error = gfs2_glock_nq_init(ip->i_gl, lmode, 0, &i_gh);
540         if (error)
541                 return error;
542
543         if (cmd == GFS2_IOCTL_SETFLAGS) {
544                 change = flags ^ ip->i_di.di_flags;
545                 error = -EPERM;
546                 if (change & (GFS2_DIF_IMMUTABLE|GFS2_DIF_APPENDONLY)) {
547                         if (!capable(CAP_LINUX_IMMUTABLE))
548                                 goto out;
549                 }
550                 error = -EINVAL;
551                 if (flags & (GFS2_DIF_JDATA|GFS2_DIF_DIRECTIO)) {
552                         if (!S_ISREG(ip->i_di.di_mode))
553                                 goto out;
554                 }
555                 if (flags & (GFS2_DIF_INHERIT_JDATA|GFS2_DIF_INHERIT_DIRECTIO)) {
556                         if (!S_ISDIR(ip->i_di.di_mode))
557                                 goto out;
558                 }
559
560                 error = gfs2_trans_begin(ip->i_sbd, RES_DINODE, 0);
561                 if (error)
562                         goto out;
563
564                 error = gfs2_meta_inode_buffer(ip, &dibh);
565                 if (error)
566                         goto out_trans_end;
567
568                 ip->i_di.di_flags = flags;
569
570                 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
571                 gfs2_dinode_out(&ip->i_di, dibh->b_data);
572
573                 brelse(dibh);
574
575 out_trans_end:
576                 gfs2_trans_end(ip->i_sbd);
577         } else {
578                 flags = ip->i_di.di_flags;
579         }
580 out:
581         gfs2_glock_dq_uninit(&i_gh);
582         if (cmd == GFS2_IOCTL_GETFLAGS) {
583                 if (put_user(flags, (__u32 __user *)arg))
584                         return -EFAULT;
585         }
586         return error;
587 }
588
589 /**
590  * gfs2_ioctl - do an ioctl on a file
591  * @inode: the inode
592  * @file: the file pointer
593  * @cmd: the ioctl command
594  * @arg: the argument
595  *
596  * Returns: errno
597  */
598
599 static int gfs2_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
600                       unsigned long arg)
601 {
602         struct gfs2_inode *ip = get_v2ip(inode);
603
604         atomic_inc(&ip->i_sbd->sd_ops_file);
605
606         switch (cmd) {
607         case GFS2_IOCTL_SETFLAGS:
608         case GFS2_IOCTL_GETFLAGS:
609                 return gfs2_ioctl_flags(ip, cmd, arg);
610
611         default:
612                 return -ENOTTY;
613         }
614 }
615
616 /**
617  * gfs2_mmap -
618  * @file: The file to map
619  * @vma: The VMA which described the mapping
620  *
621  * Returns: 0 or error code
622  */
623
624 static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
625 {
626         struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
627         struct gfs2_holder i_gh;
628         int error;
629
630         atomic_inc(&ip->i_sbd->sd_ops_file);
631
632         gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
633         error = gfs2_glock_nq_atime(&i_gh);
634         if (error) {
635                 gfs2_holder_uninit(&i_gh);
636                 return error;
637         }
638
639         /* This is VM_MAYWRITE instead of VM_WRITE because a call
640            to mprotect() can turn on VM_WRITE later. */
641
642         if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
643             (VM_MAYSHARE | VM_MAYWRITE))
644                 vma->vm_ops = &gfs2_vm_ops_sharewrite;
645         else
646                 vma->vm_ops = &gfs2_vm_ops_private;
647
648         gfs2_glock_dq_uninit(&i_gh);
649
650         return error;
651 }
652
653 /**
654  * gfs2_open - open a file
655  * @inode: the inode to open
656  * @file: the struct file for this opening
657  *
658  * Returns: errno
659  */
660
661 static int gfs2_open(struct inode *inode, struct file *file)
662 {
663         struct gfs2_inode *ip = get_v2ip(inode);
664         struct gfs2_holder i_gh;
665         struct gfs2_file *fp;
666         int error;
667
668         atomic_inc(&ip->i_sbd->sd_ops_file);
669
670         fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
671         if (!fp)
672                 return -ENOMEM;
673
674         init_MUTEX(&fp->f_fl_mutex);
675
676         fp->f_inode = ip;
677         fp->f_vfile = file;
678
679         gfs2_assert_warn(ip->i_sbd, !get_v2fp(file));
680         set_v2fp(file, fp);
681
682         if (S_ISREG(ip->i_di.di_mode)) {
683                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
684                                            &i_gh);
685                 if (error)
686                         goto fail;
687
688                 if (!(file->f_flags & O_LARGEFILE) &&
689                     ip->i_di.di_size > MAX_NON_LFS) {
690                         error = -EFBIG;
691                         goto fail_gunlock;
692                 }
693
694                 /* Listen to the Direct I/O flag */
695
696                 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
697                         file->f_flags |= O_DIRECT;
698
699                 gfs2_glock_dq_uninit(&i_gh);
700         }
701
702         return 0;
703
704  fail_gunlock:
705         gfs2_glock_dq_uninit(&i_gh);
706
707  fail:
708         set_v2fp(file, NULL);
709         kfree(fp);
710
711         return error;
712 }
713
714 /**
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
718  *
719  * Returns: errno
720  */
721
722 static int gfs2_close(struct inode *inode, struct file *file)
723 {
724         struct gfs2_sbd *sdp = get_v2sdp(inode->i_sb);
725         struct gfs2_file *fp;
726
727         atomic_inc(&sdp->sd_ops_file);
728
729         fp = get_v2fp(file);
730         set_v2fp(file, NULL);
731
732         if (gfs2_assert_warn(sdp, fp))
733                 return -EIO;
734
735         kfree(fp);
736
737         return 0;
738 }
739
740 /**
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
744  *
745  * Returns: errno
746  */
747
748 static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
749 {
750         struct gfs2_inode *ip = get_v2ip(dentry->d_inode);
751
752         atomic_inc(&ip->i_sbd->sd_ops_file);
753         gfs2_log_flush_glock(ip->i_gl);
754
755         return 0;
756 }
757
758 /**
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
763  *
764  * Returns: errno
765  */
766
767 static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
768 {
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 };
774
775         atomic_inc(&sdp->sd_ops_file);
776
777         if (!(fl->fl_flags & FL_POSIX))
778                 return -ENOLCK;
779         if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
780                 return -ENOLCK;
781
782         if (sdp->sd_args.ar_localflocks) {
783                 if (IS_GETLK(cmd)) {
784                         struct file_lock *tmp;
785                         lock_kernel();
786                         tmp = posix_test_lock(file, fl);
787                         fl->fl_type = F_UNLCK;
788                         if (tmp)
789                                 memcpy(fl, tmp, sizeof(struct file_lock));
790                         unlock_kernel();
791                         return 0;
792                 } else {
793                         int error;
794                         lock_kernel();
795                         error = posix_lock_file_wait(file, fl);
796                         unlock_kernel();
797                         return error;
798                 }
799         }
800
801         if (IS_GETLK(cmd))
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);
805         else
806                 return gfs2_lm_plock(sdp, &name, file, cmd, fl);
807 }
808
809 /**
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
815  *
816  * Outputs: offset - updated according to number of bytes read
817  *
818  * Returns: The number of bytes sent, errno on failure
819  */
820
821 static ssize_t gfs2_sendfile(struct file *in_file, loff_t *offset, size_t count,
822                              read_actor_t actor, void *target)
823 {
824         struct gfs2_inode *ip = get_v2ip(in_file->f_mapping->host);
825
826         atomic_inc(&ip->i_sbd->sd_ops_file);
827
828         return generic_file_sendfile(in_file, offset, count, actor, target);
829 }
830
831 static int do_flock(struct file *file, int cmd, struct file_lock *fl)
832 {
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;
837         unsigned int state;
838         int flags;
839         int error = 0;
840
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;
843
844         down(&fp->f_fl_mutex);
845
846         gl = fl_gh->gh_gl;
847         if (gl) {
848                 if (fl_gh->gh_state == state)
849                         goto out;
850                 gfs2_glock_hold(gl);
851                 flock_lock_file_wait(file,
852                                      &(struct file_lock){.fl_type = F_UNLCK});          
853                 gfs2_glock_dq_uninit(fl_gh);
854         } else {
855                 error = gfs2_glock_get(ip->i_sbd,
856                                       ip->i_num.no_addr, &gfs2_flock_glops,
857                                       CREATE, &gl);
858                 if (error)
859                         goto out;
860         }
861
862         gfs2_holder_init(gl, state, flags, fl_gh);
863         gfs2_glock_put(gl);
864
865         error = gfs2_glock_nq(fl_gh);
866         if (error) {
867                 gfs2_holder_uninit(fl_gh);
868                 if (error == GLR_TRYFAILED)
869                         error = -EAGAIN;
870         } else {
871                 error = flock_lock_file_wait(file, fl);
872                 gfs2_assert_warn(ip->i_sbd, !error);
873         }
874
875  out:
876         up(&fp->f_fl_mutex);
877
878         return error;
879 }
880
881 static void do_unflock(struct file *file, struct file_lock *fl)
882 {
883         struct gfs2_file *fp = get_v2fp(file);
884         struct gfs2_holder *fl_gh = &fp->f_fl_gh;
885
886         down(&fp->f_fl_mutex);
887         flock_lock_file_wait(file, fl);
888         if (fl_gh->gh_gl)
889                 gfs2_glock_dq_uninit(fl_gh);
890         up(&fp->f_fl_mutex);
891 }
892
893 /**
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
898  *
899  * Returns: errno
900  */
901
902 static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
903 {
904         struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
905         struct gfs2_sbd *sdp = ip->i_sbd;
906
907         atomic_inc(&ip->i_sbd->sd_ops_file);
908
909         if (!(fl->fl_flags & FL_FLOCK))
910                 return -ENOLCK;
911         if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
912                 return -ENOLCK;
913
914         if (sdp->sd_args.ar_localflocks)
915                 return flock_lock_file_wait(file, fl);
916
917         if (fl->fl_type == F_UNLCK) {
918                 do_unflock(file, fl);
919                 return 0;
920         } else
921                 return do_flock(file, cmd, fl);
922 }
923
924 struct file_operations gfs2_file_fops = {
925         .llseek = gfs2_llseek,
926         .read = gfs2_read,
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,
932         .ioctl = gfs2_ioctl,
933         .mmap = gfs2_mmap,
934         .open = gfs2_open,
935         .release = gfs2_close,
936         .fsync = gfs2_fsync,
937         .lock = gfs2_lock,
938         .sendfile = gfs2_sendfile,
939         .flock = gfs2_flock,
940 };
941
942 struct file_operations gfs2_dir_fops = {
943         .readdir = gfs2_readdir,
944         .ioctl = gfs2_ioctl,
945         .open = gfs2_open,
946         .release = gfs2_close,
947         .fsync = gfs2_fsync,
948         .lock = gfs2_lock,
949         .flock = gfs2_flock,
950 };
951