blob: b551074a3c98067bf428e6ccb132fcad5106b84e [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
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>
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000020#include <linux/fs.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050021#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050022#include <linux/ext2_fs.h>
23#include <linux/crc32.h>
Steven Whitehouse7ea9ea82006-03-31 15:01:28 -050024#include <linux/iflags.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000025#include <asm/uaccess.h>
26
27#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050028#include "lm_interface.h"
29#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000030#include "bmap.h"
31#include "dir.h"
32#include "glock.h"
33#include "glops.h"
34#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000035#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"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050043#include "util.h"
Steven Whitehouse71b86f52006-03-28 14:14:04 -050044#include "eaops.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000045
46/* "bad" is for NFS support */
47struct filldir_bad_entry {
48 char *fbe_name;
49 unsigned int fbe_length;
Steven Whitehousecd915492006-09-04 12:49:07 -040050 u64 fbe_offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +000051 struct gfs2_inum fbe_inum;
52 unsigned int fbe_type;
53};
54
55struct 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 */
68struct filldir_reg {
69 struct gfs2_sbd *fdr_sbd;
70 int fdr_prefetch;
71
72 filldir_t fdr_filldir;
73 void *fdr_opaque;
74};
75
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000076/*
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 */
Steven Whitehousedd538c82006-09-04 14:53:30 -040082struct file gfs2_internal_file_sentinel = {
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000083 .f_flags = O_NOATIME|O_RDONLY,
84};
85
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000086static 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);
Steven Whitehouse26c1a572006-09-04 15:32:10 -040097 kunmap(page);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000098
Steven Whitehouse26c1a572006-09-04 15:32:10 -040099 desc->count = count - size;
100 desc->written += size;
101 desc->arg.buf += size;
102 return size;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000103}
104
105int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
106 char *buf, loff_t *pos, unsigned size)
107{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400108 struct inode *inode = &ip->i_inode;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000109 read_descriptor_t desc;
110 desc.written = 0;
111 desc.arg.buf = buf;
112 desc.count = size;
113 desc.error = 0;
Steven Whitehouse61a30dc2006-02-15 10:15:18 +0000114 do_generic_mapping_read(inode->i_mapping, ra_state,
Steven Whitehousedd538c82006-09-04 14:53:30 -0400115 &gfs2_internal_file_sentinel, pos, &desc,
Steven Whitehouse61a30dc2006-02-15 10:15:18 +0000116 gfs2_read_actor);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000117 return desc.written ? desc.written : desc.error;
118}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000119
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
132static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
133{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400134 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000135 struct gfs2_holder i_gh;
136 loff_t error;
137
David Teiglandb3b94fa2006-01-16 16:50:04 +0000138 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
David Teiglandb3b94fa2006-01-16 16:50:04 +0000151/**
152 * filldir_reg_func - Report a directory entry to the caller of gfs2_dir_read()
153 * @opaque: opaque data used by the function
154 * @name: the name of the directory entry
155 * @length: the length of the name
156 * @offset: the entry's offset in the directory
157 * @inum: the inode number the entry points to
158 * @type: the type of inode the entry points to
159 *
160 * Returns: 0 on success, 1 if buffer full
161 */
162
163static int filldir_reg_func(void *opaque, const char *name, unsigned int length,
Steven Whitehousecd915492006-09-04 12:49:07 -0400164 u64 offset, struct gfs2_inum *inum,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000165 unsigned int type)
166{
167 struct filldir_reg *fdr = (struct filldir_reg *)opaque;
168 struct gfs2_sbd *sdp = fdr->fdr_sbd;
169 int error;
170
171 error = fdr->fdr_filldir(fdr->fdr_opaque, name, length, offset,
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400172 inum->no_addr, type);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000173 if (error)
174 return 1;
175
176 if (fdr->fdr_prefetch && !(length == 1 && *name == '.')) {
177 gfs2_glock_prefetch_num(sdp,
178 inum->no_addr, &gfs2_inode_glops,
179 LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
180 gfs2_glock_prefetch_num(sdp,
181 inum->no_addr, &gfs2_iopen_glops,
182 LM_ST_SHARED, LM_FLAG_TRY);
183 }
184
185 return 0;
186}
187
188/**
189 * readdir_reg - Read directory entries from a directory
190 * @file: The directory to read from
191 * @dirent: Buffer for dirents
192 * @filldir: Function used to do the copying
193 *
194 * Returns: errno
195 */
196
197static int readdir_reg(struct file *file, void *dirent, filldir_t filldir)
198{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500199 struct inode *dir = file->f_mapping->host;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400200 struct gfs2_inode *dip = GFS2_I(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000201 struct filldir_reg fdr;
202 struct gfs2_holder d_gh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400203 u64 offset = file->f_pos;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000204 int error;
205
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400206 fdr.fdr_sbd = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000207 fdr.fdr_prefetch = 1;
208 fdr.fdr_filldir = filldir;
209 fdr.fdr_opaque = dirent;
210
211 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
212 error = gfs2_glock_nq_atime(&d_gh);
213 if (error) {
214 gfs2_holder_uninit(&d_gh);
215 return error;
216 }
217
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500218 error = gfs2_dir_read(dir, &offset, &fdr, filldir_reg_func);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000219
220 gfs2_glock_dq_uninit(&d_gh);
221
222 file->f_pos = offset;
223
224 return error;
225}
226
227/**
228 * filldir_bad_func - Report a directory entry to the caller of gfs2_dir_read()
229 * @opaque: opaque data used by the function
230 * @name: the name of the directory entry
231 * @length: the length of the name
232 * @offset: the entry's offset in the directory
233 * @inum: the inode number the entry points to
234 * @type: the type of inode the entry points to
235 *
236 * For supporting NFS.
237 *
238 * Returns: 0 on success, 1 if buffer full
239 */
240
241static int filldir_bad_func(void *opaque, const char *name, unsigned int length,
Steven Whitehousecd915492006-09-04 12:49:07 -0400242 u64 offset, struct gfs2_inum *inum,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000243 unsigned int type)
244{
245 struct filldir_bad *fdb = (struct filldir_bad *)opaque;
246 struct gfs2_sbd *sdp = fdb->fdb_sbd;
247 struct filldir_bad_entry *fbe;
248
249 if (fdb->fdb_entry_off == fdb->fdb_entry_num ||
250 fdb->fdb_name_off + length > fdb->fdb_name_size)
251 return 1;
252
253 fbe = &fdb->fdb_entry[fdb->fdb_entry_off];
254 fbe->fbe_name = fdb->fdb_name + fdb->fdb_name_off;
255 memcpy(fbe->fbe_name, name, length);
256 fbe->fbe_length = length;
257 fbe->fbe_offset = offset;
258 fbe->fbe_inum = *inum;
259 fbe->fbe_type = type;
260
261 fdb->fdb_entry_off++;
262 fdb->fdb_name_off += length;
263
264 if (!(length == 1 && *name == '.')) {
265 gfs2_glock_prefetch_num(sdp,
266 inum->no_addr, &gfs2_inode_glops,
267 LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
268 gfs2_glock_prefetch_num(sdp,
269 inum->no_addr, &gfs2_iopen_glops,
270 LM_ST_SHARED, LM_FLAG_TRY);
271 }
272
273 return 0;
274}
275
276/**
277 * readdir_bad - Read directory entries from a directory
278 * @file: The directory to read from
279 * @dirent: Buffer for dirents
280 * @filldir: Function used to do the copying
281 *
282 * For supporting NFS.
283 *
284 * Returns: errno
285 */
286
287static int readdir_bad(struct file *file, void *dirent, filldir_t filldir)
288{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500289 struct inode *dir = file->f_mapping->host;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400290 struct gfs2_inode *dip = GFS2_I(dir);
291 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000292 struct filldir_reg fdr;
293 unsigned int entries, size;
294 struct filldir_bad *fdb;
295 struct gfs2_holder d_gh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400296 u64 offset = file->f_pos;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000297 unsigned int x;
298 struct filldir_bad_entry *fbe;
299 int error;
300
301 entries = gfs2_tune_get(sdp, gt_entries_per_readdir);
302 size = sizeof(struct filldir_bad) +
303 entries * (sizeof(struct filldir_bad_entry) + GFS2_FAST_NAME_SIZE);
304
305 fdb = kzalloc(size, GFP_KERNEL);
306 if (!fdb)
307 return -ENOMEM;
308
309 fdb->fdb_sbd = sdp;
310 fdb->fdb_entry = (struct filldir_bad_entry *)(fdb + 1);
311 fdb->fdb_entry_num = entries;
312 fdb->fdb_name = ((char *)fdb) + sizeof(struct filldir_bad) +
313 entries * sizeof(struct filldir_bad_entry);
314 fdb->fdb_name_size = entries * GFS2_FAST_NAME_SIZE;
315
316 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
317 error = gfs2_glock_nq_atime(&d_gh);
318 if (error) {
319 gfs2_holder_uninit(&d_gh);
320 goto out;
321 }
322
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500323 error = gfs2_dir_read(dir, &offset, fdb, filldir_bad_func);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000324
325 gfs2_glock_dq_uninit(&d_gh);
326
327 fdr.fdr_sbd = sdp;
328 fdr.fdr_prefetch = 0;
329 fdr.fdr_filldir = filldir;
330 fdr.fdr_opaque = dirent;
331
332 for (x = 0; x < fdb->fdb_entry_off; x++) {
333 fbe = &fdb->fdb_entry[x];
334
335 error = filldir_reg_func(&fdr,
336 fbe->fbe_name, fbe->fbe_length,
337 fbe->fbe_offset,
338 &fbe->fbe_inum, fbe->fbe_type);
339 if (error) {
340 file->f_pos = fbe->fbe_offset;
341 error = 0;
342 goto out;
343 }
344 }
345
346 file->f_pos = offset;
347
Steven Whitehousea91ea692006-09-04 12:04:26 -0400348out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000349 kfree(fdb);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000350 return error;
351}
352
353/**
354 * gfs2_readdir - Read directory entries from a directory
355 * @file: The directory to read from
356 * @dirent: Buffer for dirents
357 * @filldir: Function used to do the copying
358 *
359 * Returns: errno
360 */
361
362static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
363{
364 int error;
365
David Teiglandb3b94fa2006-01-16 16:50:04 +0000366 if (strcmp(current->comm, "nfsd") != 0)
367 error = readdir_reg(file, dirent, filldir);
368 else
369 error = readdir_bad(file, dirent, filldir);
370
371 return error;
372}
373
Steven Whitehouse7ea9ea82006-03-31 15:01:28 -0500374static const u32 iflags_to_gfs2[32] = {
375 [iflag_Sync] = GFS2_DIF_SYNC,
376 [iflag_Immutable] = GFS2_DIF_IMMUTABLE,
377 [iflag_Append] = GFS2_DIF_APPENDONLY,
378 [iflag_NoAtime] = GFS2_DIF_NOATIME,
379 [iflag_Index] = GFS2_DIF_EXHASH,
380 [iflag_JournalData] = GFS2_DIF_JDATA,
381 [iflag_DirectIO] = GFS2_DIF_DIRECTIO,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500382};
383
Steven Whitehouse7ea9ea82006-03-31 15:01:28 -0500384static const u32 gfs2_to_iflags[32] = {
385 [gfs2fl_Sync] = IFLAG_SYNC,
386 [gfs2fl_Immutable] = IFLAG_IMMUTABLE,
387 [gfs2fl_AppendOnly] = IFLAG_APPEND,
388 [gfs2fl_NoAtime] = IFLAG_NOATIME,
389 [gfs2fl_ExHash] = IFLAG_INDEX,
390 [gfs2fl_Jdata] = IFLAG_JOURNAL_DATA,
391 [gfs2fl_Directio] = IFLAG_DIRECTIO,
Steven Whitehouse4bcf7092006-04-25 13:20:27 -0400392 [gfs2fl_InheritDirectio] = IFLAG_DIRECTIO,
393 [gfs2fl_InheritJdata] = IFLAG_JOURNAL_DATA,
Steven Whitehouse7ea9ea82006-03-31 15:01:28 -0500394};
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500395
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400396static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500397{
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400398 struct inode *inode = filp->f_dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400399 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500400 struct gfs2_holder gh;
401 int error;
Steven Whitehouse7ea9ea82006-03-31 15:01:28 -0500402 u32 iflags;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500403
404 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
405 error = gfs2_glock_nq_m_atime(1, &gh);
406 if (error)
407 return error;
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400408
Steven Whitehouse7ea9ea82006-03-31 15:01:28 -0500409 iflags = iflags_cvt(gfs2_to_iflags, ip->i_di.di_flags);
410 if (put_user(iflags, ptr))
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500411 error = -EFAULT;
412
413 gfs2_glock_dq_m(1, &gh);
414 gfs2_holder_uninit(&gh);
415 return error;
416}
417
418/* Flags that can be set by user space */
419#define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
420 GFS2_DIF_DIRECTIO| \
421 GFS2_DIF_IMMUTABLE| \
422 GFS2_DIF_APPENDONLY| \
423 GFS2_DIF_NOATIME| \
424 GFS2_DIF_SYNC| \
425 GFS2_DIF_SYSTEM| \
426 GFS2_DIF_INHERIT_DIRECTIO| \
427 GFS2_DIF_INHERIT_JDATA)
428
429/**
430 * gfs2_set_flags - set flags on an inode
431 * @inode: The inode
432 * @flags: The flags to set
433 * @mask: Indicates which flags are valid
434 *
435 */
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400436static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500437{
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400438 struct inode *inode = filp->f_dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400439 struct gfs2_inode *ip = GFS2_I(inode);
440 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500441 struct buffer_head *bh;
442 struct gfs2_holder gh;
443 int error;
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400444 u32 new_flags, flags;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500445
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500446 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
Abhijith Das52f341c2006-07-21 02:03:21 -0400447 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500448 return error;
449
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400450 flags = ip->i_di.di_flags;
451 new_flags = (flags & ~mask) | (reqflags & mask);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500452 if ((new_flags ^ flags) == 0)
453 goto out;
454
Steven Whitehouse4bcf7092006-04-25 13:20:27 -0400455 if (S_ISDIR(inode->i_mode)) {
456 if ((new_flags ^ flags) & GFS2_DIF_JDATA)
457 new_flags ^= (GFS2_DIF_JDATA|GFS2_DIF_INHERIT_JDATA);
458 if ((new_flags ^ flags) & GFS2_DIF_DIRECTIO)
459 new_flags ^= (GFS2_DIF_DIRECTIO|GFS2_DIF_INHERIT_DIRECTIO);
460 }
461
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500462 error = -EINVAL;
463 if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
464 goto out;
465
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500466 error = -EPERM;
467 if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
468 goto out;
469 if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
470 goto out;
Steven Whitehouseb9cb9812006-05-12 17:07:56 -0400471 if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
472 !capable(CAP_LINUX_IMMUTABLE))
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500473 goto out;
Steven Whitehouseb9cb9812006-05-12 17:07:56 -0400474 if (!IS_IMMUTABLE(inode)) {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400475 error = permission(inode, MAY_WRITE, NULL);
Steven Whitehouseb9cb9812006-05-12 17:07:56 -0400476 if (error)
477 goto out;
478 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500479
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400480 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500481 if (error)
482 goto out;
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400483 error = gfs2_meta_inode_buffer(ip, &bh);
484 if (error)
485 goto out_trans_end;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500486 gfs2_trans_add_bh(ip->i_gl, bh, 1);
487 ip->i_di.di_flags = new_flags;
488 gfs2_dinode_out(&ip->i_di, bh->b_data);
489 brelse(bh);
Steven Whitehouse55eccc62006-04-04 14:29:30 -0400490out_trans_end:
491 gfs2_trans_end(sdp);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500492out:
493 gfs2_glock_dq_uninit(&gh);
494 return error;
495}
496
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400497static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500498{
Steven Whitehouse7ea9ea82006-03-31 15:01:28 -0500499 u32 iflags, gfsflags;
500 if (get_user(iflags, ptr))
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500501 return -EFAULT;
Steven Whitehouse7ea9ea82006-03-31 15:01:28 -0500502 gfsflags = iflags_cvt(iflags_to_gfs2, iflags);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400503 return do_gfs2_set_flags(filp, gfsflags, ~0);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500504}
505
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400506static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500507{
508 switch(cmd) {
Steven Whitehouse7ea9ea82006-03-31 15:01:28 -0500509 case IFLAGS_GET_IOC:
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400510 return gfs2_get_flags(filp, (u32 __user *)arg);
Steven Whitehouse7ea9ea82006-03-31 15:01:28 -0500511 case IFLAGS_SET_IOC:
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400512 return gfs2_set_flags(filp, (u32 __user *)arg);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500513 }
514 return -ENOTTY;
515}
516
517
David Teiglandb3b94fa2006-01-16 16:50:04 +0000518/**
519 * gfs2_mmap -
520 * @file: The file to map
521 * @vma: The VMA which described the mapping
522 *
523 * Returns: 0 or error code
524 */
525
526static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
527{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400528 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000529 struct gfs2_holder i_gh;
530 int error;
531
David Teiglandb3b94fa2006-01-16 16:50:04 +0000532 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
533 error = gfs2_glock_nq_atime(&i_gh);
534 if (error) {
535 gfs2_holder_uninit(&i_gh);
536 return error;
537 }
538
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000539 /* This is VM_MAYWRITE instead of VM_WRITE because a call
540 to mprotect() can turn on VM_WRITE later. */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000541
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000542 if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
543 (VM_MAYSHARE | VM_MAYWRITE))
544 vma->vm_ops = &gfs2_vm_ops_sharewrite;
545 else
546 vma->vm_ops = &gfs2_vm_ops_private;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000547
548 gfs2_glock_dq_uninit(&i_gh);
549
550 return error;
551}
552
553/**
554 * gfs2_open - open a file
555 * @inode: the inode to open
556 * @file: the struct file for this opening
557 *
558 * Returns: errno
559 */
560
561static int gfs2_open(struct inode *inode, struct file *file)
562{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400563 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000564 struct gfs2_holder i_gh;
565 struct gfs2_file *fp;
566 int error;
567
David Teiglandb3b94fa2006-01-16 16:50:04 +0000568 fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
569 if (!fp)
570 return -ENOMEM;
571
Steven Whitehousef55ab262006-02-21 12:51:39 +0000572 mutex_init(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000573
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400574 gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500575 file->private_data = fp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000576
577 if (S_ISREG(ip->i_di.di_mode)) {
578 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
579 &i_gh);
580 if (error)
581 goto fail;
582
583 if (!(file->f_flags & O_LARGEFILE) &&
584 ip->i_di.di_size > MAX_NON_LFS) {
585 error = -EFBIG;
586 goto fail_gunlock;
587 }
588
589 /* Listen to the Direct I/O flag */
590
591 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
592 file->f_flags |= O_DIRECT;
593
David Teiglandb3b94fa2006-01-16 16:50:04 +0000594 gfs2_glock_dq_uninit(&i_gh);
595 }
596
597 return 0;
598
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400599fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000600 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400601fail:
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500602 file->private_data = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000603 kfree(fp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000604 return error;
605}
606
607/**
608 * gfs2_close - called to close a struct file
609 * @inode: the inode the struct file belongs to
610 * @file: the struct file being closed
611 *
612 * Returns: errno
613 */
614
615static int gfs2_close(struct inode *inode, struct file *file)
616{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500617 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000618 struct gfs2_file *fp;
619
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500620 fp = file->private_data;
621 file->private_data = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000622
623 if (gfs2_assert_warn(sdp, fp))
624 return -EIO;
625
626 kfree(fp);
627
628 return 0;
629}
630
631/**
632 * gfs2_fsync - sync the dirty data for a file (across the cluster)
633 * @file: the file that points to the dentry (we ignore this)
634 * @dentry: the dentry that points to the inode to sync
635 *
636 * Returns: errno
637 */
638
639static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
640{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400641 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000642
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400643 gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000644
645 return 0;
646}
647
648/**
649 * gfs2_lock - acquire/release a posix lock on a file
650 * @file: the file pointer
651 * @cmd: either modify or retrieve lock state, possibly wait
652 * @fl: type and range of lock
653 *
654 * Returns: errno
655 */
656
657static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
658{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400659 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
660 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000661 struct lm_lockname name =
662 { .ln_number = ip->i_num.no_addr,
663 .ln_type = LM_TYPE_PLOCK };
664
David Teiglandb3b94fa2006-01-16 16:50:04 +0000665 if (!(fl->fl_flags & FL_POSIX))
666 return -ENOLCK;
667 if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
668 return -ENOLCK;
669
670 if (sdp->sd_args.ar_localflocks) {
671 if (IS_GETLK(cmd)) {
Steven Whitehouse8628de02006-03-31 16:48:41 -0500672 struct file_lock tmp;
673 int ret;
674 ret = posix_test_lock(file, fl, &tmp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000675 fl->fl_type = F_UNLCK;
Steven Whitehouse8628de02006-03-31 16:48:41 -0500676 if (ret)
677 memcpy(fl, &tmp, sizeof(struct file_lock));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000678 return 0;
679 } else {
Steven Whitehouse8628de02006-03-31 16:48:41 -0500680 return posix_lock_file_wait(file, fl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000681 }
682 }
683
684 if (IS_GETLK(cmd))
685 return gfs2_lm_plock_get(sdp, &name, file, fl);
686 else if (fl->fl_type == F_UNLCK)
687 return gfs2_lm_punlock(sdp, &name, file, fl);
688 else
689 return gfs2_lm_plock(sdp, &name, file, cmd, fl);
690}
691
David Teiglandb3b94fa2006-01-16 16:50:04 +0000692static int do_flock(struct file *file, int cmd, struct file_lock *fl)
693{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500694 struct gfs2_file *fp = file->private_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000695 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400696 struct gfs2_inode *ip = GFS2_I(file->f_dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000697 struct gfs2_glock *gl;
698 unsigned int state;
699 int flags;
700 int error = 0;
701
702 state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400703 flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000704
Steven Whitehousef55ab262006-02-21 12:51:39 +0000705 mutex_lock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000706
707 gl = fl_gh->gh_gl;
708 if (gl) {
709 if (fl_gh->gh_state == state)
710 goto out;
711 gfs2_glock_hold(gl);
712 flock_lock_file_wait(file,
713 &(struct file_lock){.fl_type = F_UNLCK});
714 gfs2_glock_dq_uninit(fl_gh);
715 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400716 error = gfs2_glock_get(GFS2_SB(&ip->i_inode),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000717 ip->i_num.no_addr, &gfs2_flock_glops,
718 CREATE, &gl);
719 if (error)
720 goto out;
721 }
722
723 gfs2_holder_init(gl, state, flags, fl_gh);
724 gfs2_glock_put(gl);
725
726 error = gfs2_glock_nq(fl_gh);
727 if (error) {
728 gfs2_holder_uninit(fl_gh);
729 if (error == GLR_TRYFAILED)
730 error = -EAGAIN;
731 } else {
732 error = flock_lock_file_wait(file, fl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400733 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000734 }
735
Steven Whitehouse420b9e52006-07-31 15:42:17 -0400736out:
Steven Whitehousef55ab262006-02-21 12:51:39 +0000737 mutex_unlock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000738 return error;
739}
740
741static void do_unflock(struct file *file, struct file_lock *fl)
742{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500743 struct gfs2_file *fp = file->private_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000744 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
745
Steven Whitehousef55ab262006-02-21 12:51:39 +0000746 mutex_lock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000747 flock_lock_file_wait(file, fl);
748 if (fl_gh->gh_gl)
749 gfs2_glock_dq_uninit(fl_gh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000750 mutex_unlock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000751}
752
753/**
754 * gfs2_flock - acquire/release a flock lock on a file
755 * @file: the file pointer
756 * @cmd: either modify or retrieve lock state, possibly wait
757 * @fl: type and range of lock
758 *
759 * Returns: errno
760 */
761
762static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
763{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400764 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
765 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000766
David Teiglandb3b94fa2006-01-16 16:50:04 +0000767 if (!(fl->fl_flags & FL_FLOCK))
768 return -ENOLCK;
769 if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
770 return -ENOLCK;
771
772 if (sdp->sd_args.ar_localflocks)
773 return flock_lock_file_wait(file, fl);
774
775 if (fl->fl_type == F_UNLCK) {
776 do_unflock(file, fl);
777 return 0;
778 } else
779 return do_flock(file, cmd, fl);
780}
781
Steven Whitehouseb0dd9302006-07-03 13:47:02 -0400782const struct file_operations gfs2_file_fops = {
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400783 .llseek = gfs2_llseek,
784 .read = generic_file_read,
785 .readv = generic_file_readv,
786 .aio_read = generic_file_aio_read,
787 .write = generic_file_write,
788 .writev = generic_file_writev,
789 .aio_write = generic_file_aio_write,
790 .unlocked_ioctl = gfs2_ioctl,
791 .mmap = gfs2_mmap,
792 .open = gfs2_open,
793 .release = gfs2_close,
794 .fsync = gfs2_fsync,
795 .lock = gfs2_lock,
796 .sendfile = generic_file_sendfile,
797 .flock = gfs2_flock,
798 .splice_read = generic_file_splice_read,
799 .splice_write = generic_file_splice_write,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000800};
801
Steven Whitehouseb0dd9302006-07-03 13:47:02 -0400802const struct file_operations gfs2_dir_fops = {
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400803 .readdir = gfs2_readdir,
804 .unlocked_ioctl = gfs2_ioctl,
805 .open = gfs2_open,
806 .release = gfs2_close,
807 .fsync = gfs2_fsync,
808 .lock = gfs2_lock,
809 .flock = gfs2_flock,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000810};
811