[GFS2] Further updates to dir and logging code
[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/fs.h>
21 #include <linux/gfs2_ondisk.h>
22 #include <linux/ext2_fs.h>
23 #include <linux/crc32.h>
24 #include <asm/semaphore.h>
25 #include <asm/uaccess.h>
26
27 #include "gfs2.h"
28 #include "lm_interface.h"
29 #include "incore.h"
30 #include "bmap.h"
31 #include "dir.h"
32 #include "glock.h"
33 #include "glops.h"
34 #include "inode.h"
35 #include "lm.h"
36 #include "log.h"
37 #include "meta_io.h"
38 #include "ops_file.h"
39 #include "ops_vm.h"
40 #include "quota.h"
41 #include "rgrp.h"
42 #include "trans.h"
43 #include "util.h"
44 #include "eaops.h"
45
46 /* "bad" is for NFS support */
47 struct filldir_bad_entry {
48         char *fbe_name;
49         unsigned int fbe_length;
50         uint64_t fbe_offset;
51         struct gfs2_inum fbe_inum;
52         unsigned int fbe_type;
53 };
54
55 struct filldir_bad {
56         struct gfs2_sbd *fdb_sbd;
57
58         struct filldir_bad_entry *fdb_entry;
59         unsigned int fdb_entry_num;
60         unsigned int fdb_entry_off;
61
62         char *fdb_name;
63         unsigned int fdb_name_size;
64         unsigned int fdb_name_off;
65 };
66
67 /* For regular, non-NFS */
68 struct filldir_reg {
69         struct gfs2_sbd *fdr_sbd;
70         int fdr_prefetch;
71
72         filldir_t fdr_filldir;
73         void *fdr_opaque;
74 };
75
76 /*
77  * Most fields left uninitialised to catch anybody who tries to
78  * use them. f_flags set to prevent file_accessed() from touching
79  * any other part of this. Its use is purely as a flag so that we
80  * know (in readpage()) whether or not do to locking.
81  */
82 struct file gfs2_internal_file_sentinal = {
83         .f_flags = O_NOATIME|O_RDONLY,
84 };
85
86 static int gfs2_read_actor(read_descriptor_t *desc, struct page *page,
87                            unsigned long offset, unsigned long size)
88 {
89         char *kaddr;
90         unsigned long count = desc->count;
91
92         if (size > count)
93                 size = count;
94
95         kaddr = kmap(page);
96         memcpy(desc->arg.buf, kaddr + offset, size);
97         kunmap(page);
98
99         desc->count = count - size;
100         desc->written += size;
101         desc->arg.buf += size;
102         return size;
103 }
104
105 int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
106                        char *buf, loff_t *pos, unsigned size)
107 {
108         struct inode *inode = ip->i_vnode;
109         read_descriptor_t desc;
110         desc.written = 0;
111         desc.arg.buf = buf;
112         desc.count = size;
113         desc.error = 0;
114         do_generic_mapping_read(inode->i_mapping, ra_state,
115                                 &gfs2_internal_file_sentinal, pos, &desc,
116                                 gfs2_read_actor);
117         return desc.written ? desc.written : desc.error;
118 }
119
120 /**
121  * gfs2_llseek - seek to a location in a file
122  * @file: the file
123  * @offset: the offset
124  * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
125  *
126  * SEEK_END requires the glock for the file because it references the
127  * file's size.
128  *
129  * Returns: The new offset, or errno
130  */
131
132 static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
133 {
134         struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip;
135         struct gfs2_holder i_gh;
136         loff_t error;
137
138         if (origin == 2) {
139                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
140                                            &i_gh);
141                 if (!error) {
142                         error = remote_llseek(file, offset, origin);
143                         gfs2_glock_dq_uninit(&i_gh);
144                 }
145         } else
146                 error = remote_llseek(file, offset, origin);
147
148         return error;
149 }
150
151
152 static ssize_t gfs2_direct_IO_read(struct kiocb *iocb, const struct iovec *iov,
153                                    loff_t offset, unsigned long nr_segs)
154 {
155         struct file *file = iocb->ki_filp;
156         struct address_space *mapping = file->f_mapping;
157         ssize_t retval;
158
159         retval = filemap_write_and_wait(mapping);
160         if (retval == 0) {
161                 retval = mapping->a_ops->direct_IO(READ, iocb, iov, offset,
162                                                    nr_segs);
163         }
164         return retval;
165 }
166
167 /**
168  * __gfs2_file_aio_read - The main GFS2 read function
169  * 
170  * N.B. This is almost, but not quite the same as __generic_file_aio_read()
171  * the important subtle different being that inode->i_size isn't valid
172  * unless we are holding a lock, and we do this _only_ on the O_DIRECT
173  * path since otherwise locking is done entirely at the page cache
174  * layer.
175  */
176 static ssize_t __gfs2_file_aio_read(struct kiocb *iocb,
177                                     const struct iovec *iov,
178                                     unsigned long nr_segs, loff_t *ppos)
179 {
180         struct file *filp = iocb->ki_filp;
181         struct gfs2_inode *ip = filp->f_mapping->host->u.generic_ip;
182         struct gfs2_holder gh;
183         ssize_t retval;
184         unsigned long seg;
185         size_t count;
186
187         count = 0;
188         for (seg = 0; seg < nr_segs; seg++) {
189                 const struct iovec *iv = &iov[seg];
190
191                 /*
192                  * If any segment has a negative length, or the cumulative
193                  * length ever wraps negative then return -EINVAL.
194                  */
195                 count += iv->iov_len;
196                 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
197                         return -EINVAL;
198                 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
199                         continue;
200                 if (seg == 0)
201                         return -EFAULT;
202                 nr_segs = seg;
203                 count -= iv->iov_len;   /* This segment is no good */
204                 break;
205         }
206
207         /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
208         if (filp->f_flags & O_DIRECT) {
209                 loff_t pos = *ppos, size;
210                 struct address_space *mapping;
211                 struct inode *inode;
212
213                 mapping = filp->f_mapping;
214                 inode = mapping->host;
215                 retval = 0;
216                 if (!count)
217                         goto out; /* skip atime */
218
219                 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
220                 retval = gfs2_glock_nq_m_atime(1, &gh);
221                 if (retval)
222                         goto out;
223                 if (gfs2_is_stuffed(ip)) {
224                         gfs2_glock_dq_m(1, &gh);
225                         gfs2_holder_uninit(&gh);
226                         goto fallback_to_normal;
227                 }
228                 size = i_size_read(inode);
229                 if (pos < size) {
230                         retval = gfs2_direct_IO_read(iocb, iov, pos, nr_segs);
231                         if (retval > 0 && !is_sync_kiocb(iocb))
232                                 retval = -EIOCBQUEUED;
233                         if (retval > 0)
234                                 *ppos = pos + retval;
235                 }
236                 file_accessed(filp);
237                 gfs2_glock_dq_m(1, &gh);
238                 gfs2_holder_uninit(&gh);
239                 goto out;
240         }
241
242 fallback_to_normal:
243         retval = 0;
244         if (count) {
245                 for (seg = 0; seg < nr_segs; seg++) {
246                         read_descriptor_t desc;
247
248                         desc.written = 0;
249                         desc.arg.buf = iov[seg].iov_base;
250                         desc.count = iov[seg].iov_len;
251                         if (desc.count == 0)
252                                 continue;
253                         desc.error = 0;
254                         do_generic_file_read(filp,ppos,&desc,file_read_actor);
255                         retval += desc.written;
256                         if (desc.error) {
257                                 retval = retval ?: desc.error;
258                                  break;
259                         }
260                 }
261         }
262 out:
263         return retval;
264 }
265
266 /**
267  * gfs2_read - Read bytes from a file
268  * @file: The file to read from
269  * @buf: The buffer to copy into
270  * @size: The amount of data requested
271  * @offset: The current file offset
272  *
273  * Outputs: Offset - updated according to number of bytes read
274  *
275  * Returns: The number of bytes read, errno on failure
276  */
277
278 static ssize_t gfs2_read(struct file *filp, char __user *buf, size_t size,
279                          loff_t *offset)
280 {
281         struct iovec local_iov = { .iov_base = buf, .iov_len = size };
282         struct kiocb kiocb;
283         ssize_t ret;
284
285         init_sync_kiocb(&kiocb, filp);
286         ret = __gfs2_file_aio_read(&kiocb, &local_iov, 1, offset);
287         if (-EIOCBQUEUED == ret)
288                 ret = wait_on_sync_kiocb(&kiocb);
289         return ret;
290 }
291
292 static ssize_t gfs2_file_readv(struct file *filp, const struct iovec *iov,
293                                unsigned long nr_segs, loff_t *ppos)
294 {
295         struct kiocb kiocb;
296         ssize_t ret;
297
298         init_sync_kiocb(&kiocb, filp);
299         ret = __gfs2_file_aio_read(&kiocb, iov, nr_segs, ppos);
300         if (-EIOCBQUEUED == ret)
301                 ret = wait_on_sync_kiocb(&kiocb);
302         return ret;
303 }
304
305 static ssize_t gfs2_file_aio_read(struct kiocb *iocb, char __user *buf,
306                                   size_t count, loff_t pos)
307 {
308         struct iovec local_iov = { .iov_base = buf, .iov_len = count };
309
310         BUG_ON(iocb->ki_pos != pos);
311         return __gfs2_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos);
312 }
313
314
315 /**
316  * filldir_reg_func - Report a directory entry to the caller of gfs2_dir_read()
317  * @opaque: opaque data used by the function
318  * @name: the name of the directory entry
319  * @length: the length of the name
320  * @offset: the entry's offset in the directory
321  * @inum: the inode number the entry points to
322  * @type: the type of inode the entry points to
323  *
324  * Returns: 0 on success, 1 if buffer full
325  */
326
327 static int filldir_reg_func(void *opaque, const char *name, unsigned int length,
328                             uint64_t offset, struct gfs2_inum *inum,
329                             unsigned int type)
330 {
331         struct filldir_reg *fdr = (struct filldir_reg *)opaque;
332         struct gfs2_sbd *sdp = fdr->fdr_sbd;
333         int error;
334
335         error = fdr->fdr_filldir(fdr->fdr_opaque, name, length, offset,
336                                  inum->no_formal_ino, type);
337         if (error)
338                 return 1;
339
340         if (fdr->fdr_prefetch && !(length == 1 && *name == '.')) {
341                 gfs2_glock_prefetch_num(sdp,
342                                        inum->no_addr, &gfs2_inode_glops,
343                                        LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
344                 gfs2_glock_prefetch_num(sdp,
345                                        inum->no_addr, &gfs2_iopen_glops,
346                                        LM_ST_SHARED, LM_FLAG_TRY);
347         }
348
349         return 0;
350 }
351
352 /**
353  * readdir_reg - Read directory entries from a directory
354  * @file: The directory to read from
355  * @dirent: Buffer for dirents
356  * @filldir: Function used to do the copying
357  *
358  * Returns: errno
359  */
360
361 static int readdir_reg(struct file *file, void *dirent, filldir_t filldir)
362 {
363         struct inode *dir = file->f_mapping->host;
364         struct gfs2_inode *dip = dir->u.generic_ip;
365         struct filldir_reg fdr;
366         struct gfs2_holder d_gh;
367         uint64_t offset = file->f_pos;
368         int error;
369
370         fdr.fdr_sbd = dip->i_sbd;
371         fdr.fdr_prefetch = 1;
372         fdr.fdr_filldir = filldir;
373         fdr.fdr_opaque = dirent;
374
375         gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
376         error = gfs2_glock_nq_atime(&d_gh);
377         if (error) {
378                 gfs2_holder_uninit(&d_gh);
379                 return error;
380         }
381
382         error = gfs2_dir_read(dir, &offset, &fdr, filldir_reg_func);
383
384         gfs2_glock_dq_uninit(&d_gh);
385
386         file->f_pos = offset;
387
388         return error;
389 }
390
391 /**
392  * filldir_bad_func - Report a directory entry to the caller of gfs2_dir_read()
393  * @opaque: opaque data used by the function
394  * @name: the name of the directory entry
395  * @length: the length of the name
396  * @offset: the entry's offset in the directory
397  * @inum: the inode number the entry points to
398  * @type: the type of inode the entry points to
399  *
400  * For supporting NFS.
401  *
402  * Returns: 0 on success, 1 if buffer full
403  */
404
405 static int filldir_bad_func(void *opaque, const char *name, unsigned int length,
406                             uint64_t offset, struct gfs2_inum *inum,
407                             unsigned int type)
408 {
409         struct filldir_bad *fdb = (struct filldir_bad *)opaque;
410         struct gfs2_sbd *sdp = fdb->fdb_sbd;
411         struct filldir_bad_entry *fbe;
412
413         if (fdb->fdb_entry_off == fdb->fdb_entry_num ||
414             fdb->fdb_name_off + length > fdb->fdb_name_size)
415                 return 1;
416
417         fbe = &fdb->fdb_entry[fdb->fdb_entry_off];
418         fbe->fbe_name = fdb->fdb_name + fdb->fdb_name_off;
419         memcpy(fbe->fbe_name, name, length);
420         fbe->fbe_length = length;
421         fbe->fbe_offset = offset;
422         fbe->fbe_inum = *inum;
423         fbe->fbe_type = type;
424
425         fdb->fdb_entry_off++;
426         fdb->fdb_name_off += length;
427
428         if (!(length == 1 && *name == '.')) {
429                 gfs2_glock_prefetch_num(sdp,
430                                        inum->no_addr, &gfs2_inode_glops,
431                                        LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
432                 gfs2_glock_prefetch_num(sdp,
433                                        inum->no_addr, &gfs2_iopen_glops,
434                                        LM_ST_SHARED, LM_FLAG_TRY);
435         }
436
437         return 0;
438 }
439
440 /**
441  * readdir_bad - Read directory entries from a directory
442  * @file: The directory to read from
443  * @dirent: Buffer for dirents
444  * @filldir: Function used to do the copying
445  *
446  * For supporting NFS.
447  *
448  * Returns: errno
449  */
450
451 static int readdir_bad(struct file *file, void *dirent, filldir_t filldir)
452 {
453         struct inode *dir = file->f_mapping->host;
454         struct gfs2_inode *dip = dir->u.generic_ip;
455         struct gfs2_sbd *sdp = dip->i_sbd;
456         struct filldir_reg fdr;
457         unsigned int entries, size;
458         struct filldir_bad *fdb;
459         struct gfs2_holder d_gh;
460         uint64_t offset = file->f_pos;
461         unsigned int x;
462         struct filldir_bad_entry *fbe;
463         int error;
464
465         entries = gfs2_tune_get(sdp, gt_entries_per_readdir);
466         size = sizeof(struct filldir_bad) +
467             entries * (sizeof(struct filldir_bad_entry) + GFS2_FAST_NAME_SIZE);
468
469         fdb = kzalloc(size, GFP_KERNEL);
470         if (!fdb)
471                 return -ENOMEM;
472
473         fdb->fdb_sbd = sdp;
474         fdb->fdb_entry = (struct filldir_bad_entry *)(fdb + 1);
475         fdb->fdb_entry_num = entries;
476         fdb->fdb_name = ((char *)fdb) + sizeof(struct filldir_bad) +
477                 entries * sizeof(struct filldir_bad_entry);
478         fdb->fdb_name_size = entries * GFS2_FAST_NAME_SIZE;
479
480         gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
481         error = gfs2_glock_nq_atime(&d_gh);
482         if (error) {
483                 gfs2_holder_uninit(&d_gh);
484                 goto out;
485         }
486
487         error = gfs2_dir_read(dir, &offset, fdb, filldir_bad_func);
488
489         gfs2_glock_dq_uninit(&d_gh);
490
491         fdr.fdr_sbd = sdp;
492         fdr.fdr_prefetch = 0;
493         fdr.fdr_filldir = filldir;
494         fdr.fdr_opaque = dirent;
495
496         for (x = 0; x < fdb->fdb_entry_off; x++) {
497                 fbe = &fdb->fdb_entry[x];
498
499                 error = filldir_reg_func(&fdr,
500                                          fbe->fbe_name, fbe->fbe_length,
501                                          fbe->fbe_offset,
502                                          &fbe->fbe_inum, fbe->fbe_type);
503                 if (error) {
504                         file->f_pos = fbe->fbe_offset;
505                         error = 0;
506                         goto out;
507                 }
508         }
509
510         file->f_pos = offset;
511
512  out:
513         kfree(fdb);
514
515         return error;
516 }
517
518 /**
519  * gfs2_readdir - Read directory entries from a directory
520  * @file: The directory to read from
521  * @dirent: Buffer for dirents
522  * @filldir: Function used to do the copying
523  *
524  * Returns: errno
525  */
526
527 static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
528 {
529         int error;
530
531         if (strcmp(current->comm, "nfsd") != 0)
532                 error = readdir_reg(file, dirent, filldir);
533         else
534                 error = readdir_bad(file, dirent, filldir);
535
536         return error;
537 }
538
539 const struct gfs2_flag_eattr {
540         u32 flag;
541         u32 ext2;
542 } gfs2_flag_eattrs[] = {
543         {
544                 .flag = GFS2_DIF_IMMUTABLE,
545                 .ext2 = EXT2_IMMUTABLE_FL,
546         }, {
547                 .flag = GFS2_DIF_APPENDONLY,
548                 .ext2 = EXT2_APPEND_FL,
549         }, {
550                 .flag = GFS2_DIF_JDATA,
551                 .ext2 = EXT2_JOURNAL_DATA_FL,
552         }, {
553                 .flag = GFS2_DIF_EXHASH,
554                 .ext2 = EXT2_INDEX_FL,
555         }, {
556                 .flag = GFS2_DIF_EA_INDIRECT,
557         }, {
558                 .flag = GFS2_DIF_DIRECTIO,
559         }, {
560                 .flag = GFS2_DIF_NOATIME,
561                 .ext2 = EXT2_NOATIME_FL,
562         }, {
563                 .flag = GFS2_DIF_SYNC,
564                 .ext2 = EXT2_SYNC_FL,
565         }, {
566                 .flag = GFS2_DIF_SYSTEM,
567         }, {
568                 .flag = GFS2_DIF_TRUNC_IN_PROG,
569         }, {
570                 .flag = GFS2_DIF_INHERIT_JDATA,
571         }, {
572                 .flag = GFS2_DIF_INHERIT_DIRECTIO,
573         }, {
574         },
575 };
576
577 static const struct gfs2_flag_eattr *get_by_ext2(u32 ext2)
578 {
579         const struct gfs2_flag_eattr *p = gfs2_flag_eattrs;
580         for(; p->flag; p++) {
581                 if (ext2 == p->ext2)
582                         return p;
583         }
584         return NULL;
585 }
586
587 static const struct gfs2_flag_eattr *get_by_gfs2(u32 gfs2)
588 {
589         const struct gfs2_flag_eattr *p = gfs2_flag_eattrs;
590         for(; p->flag; p++) {
591                 if (gfs2 == p->flag)
592                         return p;
593         }
594         return NULL;
595 }
596
597 static u32 gfs2_flags_to_ext2(u32 gfs2)
598 {
599         const struct gfs2_flag_eattr *ea;
600         u32 ext2 = 0;
601         u32 mask = 1;
602
603         for(; mask != 0; mask <<=1) {
604                 if (mask & gfs2) {
605                         ea = get_by_gfs2(mask);
606                         if (ea)
607                                 ext2 |= ea->ext2;
608                 }
609         }
610         return ext2;
611 }
612
613 static int gfs2_flags_from_ext2(u32 *gfs2, u32 ext2)
614 {
615         const struct gfs2_flag_eattr *ea;
616         u32 mask = 1;
617
618         for(; mask != 0; mask <<= 1) {
619                 if (mask & ext2) {
620                         ea = get_by_ext2(mask);
621                         if (ea == NULL)
622                                 return -EINVAL;
623                         *gfs2 |= ea->flag;
624                 }
625         }
626         return 0;
627 }
628
629 static int get_ext2_flags(struct inode *inode, u32 __user *ptr)
630 {
631         struct gfs2_inode *ip = inode->u.generic_ip;
632         struct gfs2_holder gh;
633         int error;
634         u32 ext2;
635
636         gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
637         error = gfs2_glock_nq_m_atime(1, &gh);
638         if (error)
639                 return error;
640
641         ext2 = gfs2_flags_to_ext2(ip->i_di.di_flags);
642         if (put_user(ext2, ptr))
643                 error = -EFAULT;
644
645         gfs2_glock_dq_m(1, &gh);
646         gfs2_holder_uninit(&gh);
647         return error;
648 }
649
650 /* Flags that can be set by user space */
651 #define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA|                    \
652                              GFS2_DIF_DIRECTIO|                 \
653                              GFS2_DIF_IMMUTABLE|                \
654                              GFS2_DIF_APPENDONLY|               \
655                              GFS2_DIF_NOATIME|                  \
656                              GFS2_DIF_SYNC|                     \
657                              GFS2_DIF_SYSTEM|                   \
658                              GFS2_DIF_INHERIT_DIRECTIO|         \
659                              GFS2_DIF_INHERIT_JDATA)
660
661 /**
662  * gfs2_set_flags - set flags on an inode
663  * @inode: The inode
664  * @flags: The flags to set
665  * @mask: Indicates which flags are valid
666  *
667  */
668 static int gfs2_set_flags(struct inode *inode, u32 flags, u32 mask)
669 {
670         struct gfs2_inode *ip = inode->u.generic_ip;
671         struct buffer_head *bh;
672         struct gfs2_holder gh;
673         int error;
674         u32 new_flags;
675
676         gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
677         error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
678         if (error)
679                 return error;
680
681         new_flags = (ip->i_di.di_flags & ~mask) | (flags & mask);
682         if ((new_flags ^ flags) == 0)
683                 goto out;
684
685         error = -EINVAL;
686         if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
687                 goto out;
688
689         if (S_ISDIR(inode->i_mode)) {
690                 if ((new_flags ^ flags) & (GFS2_DIF_JDATA | GFS2_DIF_DIRECTIO))
691                         goto out;
692         } else if (S_ISREG(inode->i_mode)) {
693                 if ((new_flags ^ flags) & (GFS2_DIF_INHERIT_DIRECTIO|
694                                            GFS2_DIF_INHERIT_JDATA))
695                         goto out;
696         } else
697                 goto out;
698
699         error = -EPERM;
700         if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
701                 goto out;
702         if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
703                 goto out;
704         error = gfs2_repermission(inode, MAY_WRITE, NULL);
705         if (error)
706                 goto out;
707
708         error = gfs2_meta_inode_buffer(ip, &bh);
709         if (error)
710                 goto out;
711         gfs2_trans_add_bh(ip->i_gl, bh, 1);
712         ip->i_di.di_flags = new_flags;
713         gfs2_dinode_out(&ip->i_di, bh->b_data);
714         brelse(bh);
715 out:
716         gfs2_glock_dq_uninit(&gh);
717         return error;
718 }
719
720 static int set_ext2_flags(struct inode *inode, u32 __user *ptr)
721 {
722         u32 ext2, gfs2;
723         if (get_user(ext2, ptr))
724                 return -EFAULT;
725         if (gfs2_flags_from_ext2(&gfs2, ext2))
726                 return -EINVAL;
727         return gfs2_set_flags(inode, gfs2, ~0);
728 }
729
730 int gfs2_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
731                unsigned long arg)
732 {
733         switch(cmd) {
734         case EXT2_IOC_GETFLAGS:
735                 return get_ext2_flags(inode, (u32 __user *)arg);
736         case EXT2_IOC_SETFLAGS:
737                 return set_ext2_flags(inode, (u32 __user *)arg);
738         }
739         return -ENOTTY;
740 }
741
742
743 /**
744  * gfs2_mmap -
745  * @file: The file to map
746  * @vma: The VMA which described the mapping
747  *
748  * Returns: 0 or error code
749  */
750
751 static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
752 {
753         struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip;
754         struct gfs2_holder i_gh;
755         int error;
756
757         gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
758         error = gfs2_glock_nq_atime(&i_gh);
759         if (error) {
760                 gfs2_holder_uninit(&i_gh);
761                 return error;
762         }
763
764         /* This is VM_MAYWRITE instead of VM_WRITE because a call
765            to mprotect() can turn on VM_WRITE later. */
766
767         if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
768             (VM_MAYSHARE | VM_MAYWRITE))
769                 vma->vm_ops = &gfs2_vm_ops_sharewrite;
770         else
771                 vma->vm_ops = &gfs2_vm_ops_private;
772
773         gfs2_glock_dq_uninit(&i_gh);
774
775         return error;
776 }
777
778 /**
779  * gfs2_open - open a file
780  * @inode: the inode to open
781  * @file: the struct file for this opening
782  *
783  * Returns: errno
784  */
785
786 static int gfs2_open(struct inode *inode, struct file *file)
787 {
788         struct gfs2_inode *ip = inode->u.generic_ip;
789         struct gfs2_holder i_gh;
790         struct gfs2_file *fp;
791         int error;
792
793         fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
794         if (!fp)
795                 return -ENOMEM;
796
797         mutex_init(&fp->f_fl_mutex);
798
799         fp->f_inode = ip;
800         fp->f_vfile = file;
801
802         gfs2_assert_warn(ip->i_sbd, !file->private_data);
803         file->private_data = fp;
804
805         if (S_ISREG(ip->i_di.di_mode)) {
806                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
807                                            &i_gh);
808                 if (error)
809                         goto fail;
810
811                 if (!(file->f_flags & O_LARGEFILE) &&
812                     ip->i_di.di_size > MAX_NON_LFS) {
813                         error = -EFBIG;
814                         goto fail_gunlock;
815                 }
816
817                 /* Listen to the Direct I/O flag */
818
819                 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
820                         file->f_flags |= O_DIRECT;
821
822                 gfs2_glock_dq_uninit(&i_gh);
823         }
824
825         return 0;
826
827  fail_gunlock:
828         gfs2_glock_dq_uninit(&i_gh);
829
830  fail:
831         file->private_data = NULL;
832         kfree(fp);
833
834         return error;
835 }
836
837 /**
838  * gfs2_close - called to close a struct file
839  * @inode: the inode the struct file belongs to
840  * @file: the struct file being closed
841  *
842  * Returns: errno
843  */
844
845 static int gfs2_close(struct inode *inode, struct file *file)
846 {
847         struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
848         struct gfs2_file *fp;
849
850         fp = file->private_data;
851         file->private_data = NULL;
852
853         if (gfs2_assert_warn(sdp, fp))
854                 return -EIO;
855
856         kfree(fp);
857
858         return 0;
859 }
860
861 /**
862  * gfs2_fsync - sync the dirty data for a file (across the cluster)
863  * @file: the file that points to the dentry (we ignore this)
864  * @dentry: the dentry that points to the inode to sync
865  *
866  * Returns: errno
867  */
868
869 static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
870 {
871         struct gfs2_inode *ip = dentry->d_inode->u.generic_ip;
872
873         gfs2_log_flush_glock(ip->i_gl);
874
875         return 0;
876 }
877
878 /**
879  * gfs2_lock - acquire/release a posix lock on a file
880  * @file: the file pointer
881  * @cmd: either modify or retrieve lock state, possibly wait
882  * @fl: type and range of lock
883  *
884  * Returns: errno
885  */
886
887 static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
888 {
889         struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip;
890         struct gfs2_sbd *sdp = ip->i_sbd;
891         struct lm_lockname name =
892                 { .ln_number = ip->i_num.no_addr,
893                   .ln_type = LM_TYPE_PLOCK };
894
895         if (!(fl->fl_flags & FL_POSIX))
896                 return -ENOLCK;
897         if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
898                 return -ENOLCK;
899
900         if (sdp->sd_args.ar_localflocks) {
901                 if (IS_GETLK(cmd)) {
902                         struct file_lock *tmp;
903                         lock_kernel();
904                         tmp = posix_test_lock(file, fl);
905                         fl->fl_type = F_UNLCK;
906                         if (tmp)
907                                 memcpy(fl, tmp, sizeof(struct file_lock));
908                         unlock_kernel();
909                         return 0;
910                 } else {
911                         int error;
912                         lock_kernel();
913                         error = posix_lock_file_wait(file, fl);
914                         unlock_kernel();
915                         return error;
916                 }
917         }
918
919         if (IS_GETLK(cmd))
920                 return gfs2_lm_plock_get(sdp, &name, file, fl);
921         else if (fl->fl_type == F_UNLCK)
922                 return gfs2_lm_punlock(sdp, &name, file, fl);
923         else
924                 return gfs2_lm_plock(sdp, &name, file, cmd, fl);
925 }
926
927 /**
928  * gfs2_sendfile - Send bytes to a file or socket
929  * @in_file: The file to read from
930  * @out_file: The file to write to
931  * @count: The amount of data
932  * @offset: The beginning file offset
933  *
934  * Outputs: offset - updated according to number of bytes read
935  *
936  * Returns: The number of bytes sent, errno on failure
937  */
938
939 static ssize_t gfs2_sendfile(struct file *in_file, loff_t *offset, size_t count,
940                              read_actor_t actor, void *target)
941 {
942         return generic_file_sendfile(in_file, offset, count, actor, target);
943 }
944
945 static int do_flock(struct file *file, int cmd, struct file_lock *fl)
946 {
947         struct gfs2_file *fp = file->private_data;
948         struct gfs2_holder *fl_gh = &fp->f_fl_gh;
949         struct gfs2_inode *ip = fp->f_inode;
950         struct gfs2_glock *gl;
951         unsigned int state;
952         int flags;
953         int error = 0;
954
955         state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
956         flags = ((IS_SETLKW(cmd)) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
957
958         mutex_lock(&fp->f_fl_mutex);
959
960         gl = fl_gh->gh_gl;
961         if (gl) {
962                 if (fl_gh->gh_state == state)
963                         goto out;
964                 gfs2_glock_hold(gl);
965                 flock_lock_file_wait(file,
966                                      &(struct file_lock){.fl_type = F_UNLCK});          
967                 gfs2_glock_dq_uninit(fl_gh);
968         } else {
969                 error = gfs2_glock_get(ip->i_sbd,
970                                       ip->i_num.no_addr, &gfs2_flock_glops,
971                                       CREATE, &gl);
972                 if (error)
973                         goto out;
974         }
975
976         gfs2_holder_init(gl, state, flags, fl_gh);
977         gfs2_glock_put(gl);
978
979         error = gfs2_glock_nq(fl_gh);
980         if (error) {
981                 gfs2_holder_uninit(fl_gh);
982                 if (error == GLR_TRYFAILED)
983                         error = -EAGAIN;
984         } else {
985                 error = flock_lock_file_wait(file, fl);
986                 gfs2_assert_warn(ip->i_sbd, !error);
987         }
988
989  out:
990         mutex_unlock(&fp->f_fl_mutex);
991
992         return error;
993 }
994
995 static void do_unflock(struct file *file, struct file_lock *fl)
996 {
997         struct gfs2_file *fp = file->private_data;
998         struct gfs2_holder *fl_gh = &fp->f_fl_gh;
999
1000         mutex_lock(&fp->f_fl_mutex);
1001         flock_lock_file_wait(file, fl);
1002         if (fl_gh->gh_gl)
1003                 gfs2_glock_dq_uninit(fl_gh);
1004         mutex_unlock(&fp->f_fl_mutex);
1005 }
1006
1007 /**
1008  * gfs2_flock - acquire/release a flock lock on a file
1009  * @file: the file pointer
1010  * @cmd: either modify or retrieve lock state, possibly wait
1011  * @fl: type and range of lock
1012  *
1013  * Returns: errno
1014  */
1015
1016 static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
1017 {
1018         struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip;
1019         struct gfs2_sbd *sdp = ip->i_sbd;
1020
1021         if (!(fl->fl_flags & FL_FLOCK))
1022                 return -ENOLCK;
1023         if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
1024                 return -ENOLCK;
1025
1026         if (sdp->sd_args.ar_localflocks)
1027                 return flock_lock_file_wait(file, fl);
1028
1029         if (fl->fl_type == F_UNLCK) {
1030                 do_unflock(file, fl);
1031                 return 0;
1032         } else
1033                 return do_flock(file, cmd, fl);
1034 }
1035
1036 struct file_operations gfs2_file_fops = {
1037         .llseek = gfs2_llseek,
1038         .read = gfs2_read,
1039         .readv = gfs2_file_readv,
1040         .aio_read = gfs2_file_aio_read,
1041         .write = generic_file_write,
1042         .writev = generic_file_writev,
1043         .aio_write = generic_file_aio_write,
1044         .ioctl = gfs2_ioctl,
1045         .mmap = gfs2_mmap,
1046         .open = gfs2_open,
1047         .release = gfs2_close,
1048         .fsync = gfs2_fsync,
1049         .lock = gfs2_lock,
1050         .sendfile = gfs2_sendfile,
1051         .flock = gfs2_flock,
1052 };
1053
1054 struct file_operations gfs2_dir_fops = {
1055         .readdir = gfs2_readdir,
1056         .ioctl = gfs2_ioctl,
1057         .open = gfs2_open,
1058         .release = gfs2_close,
1059         .fsync = gfs2_fsync,
1060         .lock = gfs2_lock,
1061         .flock = gfs2_flock,
1062 };
1063