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