blob: e1fcef2eb9285a6f8f449b72deb1e925c540671d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_dir2.h"
25#include "xfs_trans.h"
26#include "xfs_dmapi.h"
27#include "xfs_mount.h"
28#include "xfs_bmap_btree.h"
29#include "xfs_alloc_btree.h"
30#include "xfs_ialloc_btree.h"
31#include "xfs_alloc.h"
32#include "xfs_btree.h"
33#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dir2_sf.h"
35#include "xfs_dinode.h"
36#include "xfs_inode.h"
37#include "xfs_error.h"
38#include "xfs_rw.h"
39#include "xfs_ioctl32.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100040#include "xfs_vnodeops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <linux/dcache.h>
43#include <linux/smp_lock.h>
44
Nathan Scott3562fd42006-03-14 14:00:35 +110045static struct vm_operations_struct xfs_file_vm_ops;
Dean Roehrich6fac0cb2005-06-21 14:07:45 +100046#ifdef CONFIG_XFS_DMAPI
Nathan Scott3562fd42006-03-14 14:00:35 +110047static struct vm_operations_struct xfs_dmapi_file_vm_ops;
Dean Roehrich6fac0cb2005-06-21 14:07:45 +100048#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
David Chinner7989cb82007-02-10 18:34:56 +110050STATIC_INLINE ssize_t
Nathan Scott3562fd42006-03-14 14:00:35 +110051__xfs_file_read(
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -070053 const struct iovec *iov,
54 unsigned long nr_segs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 int ioflags,
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 loff_t pos)
57{
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 BUG_ON(iocb->ki_pos != pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 if (unlikely(file->f_flags & O_DIRECT))
62 ioflags |= IO_ISDIRECT;
Christoph Hellwig739bfb22007-08-29 10:58:01 +100063 return xfs_read(XFS_I(file->f_path.dentry->d_inode), iocb, iov,
64 nr_segs, &iocb->ki_pos, ioflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067STATIC ssize_t
Nathan Scott3562fd42006-03-14 14:00:35 +110068xfs_file_aio_read(
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -070070 const struct iovec *iov,
71 unsigned long nr_segs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 loff_t pos)
73{
Badari Pulavarty027445c2006-09-30 23:28:46 -070074 return __xfs_file_read(iocb, iov, nr_segs, IO_ISAIO, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
77STATIC ssize_t
Nathan Scott3562fd42006-03-14 14:00:35 +110078xfs_file_aio_read_invis(
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -070080 const struct iovec *iov,
81 unsigned long nr_segs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 loff_t pos)
83{
Badari Pulavarty027445c2006-09-30 23:28:46 -070084 return __xfs_file_read(iocb, iov, nr_segs, IO_ISAIO|IO_INVIS, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
David Chinner7989cb82007-02-10 18:34:56 +110087STATIC_INLINE ssize_t
Nathan Scott3562fd42006-03-14 14:00:35 +110088__xfs_file_write(
Badari Pulavarty027445c2006-09-30 23:28:46 -070089 struct kiocb *iocb,
90 const struct iovec *iov,
91 unsigned long nr_segs,
92 int ioflags,
93 loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 BUG_ON(iocb->ki_pos != pos);
98 if (unlikely(file->f_flags & O_DIRECT))
99 ioflags |= IO_ISDIRECT;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000100 return xfs_write(XFS_I(file->f_mapping->host), iocb, iov, nr_segs,
101 &iocb->ki_pos, ioflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104STATIC ssize_t
Nathan Scott3562fd42006-03-14 14:00:35 +1100105xfs_file_aio_write(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700107 const struct iovec *iov,
108 unsigned long nr_segs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 loff_t pos)
110{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700111 return __xfs_file_write(iocb, iov, nr_segs, IO_ISAIO, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114STATIC ssize_t
Nathan Scott3562fd42006-03-14 14:00:35 +1100115xfs_file_aio_write_invis(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700117 const struct iovec *iov,
118 unsigned long nr_segs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 loff_t pos)
120{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700121 return __xfs_file_write(iocb, iov, nr_segs, IO_ISAIO|IO_INVIS, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124STATIC ssize_t
Nathan Scott1b895842006-03-31 13:08:59 +1000125xfs_file_splice_read(
126 struct file *infilp,
Jens Axboecbb7e572006-04-11 14:57:50 +0200127 loff_t *ppos,
Ingo Molnar3a326a22006-04-10 15:18:35 +0200128 struct pipe_inode_info *pipe,
Nathan Scott1b895842006-03-31 13:08:59 +1000129 size_t len,
130 unsigned int flags)
131{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000132 return xfs_splice_read(XFS_I(infilp->f_path.dentry->d_inode),
133 infilp, ppos, pipe, len, flags, 0);
Nathan Scott1b895842006-03-31 13:08:59 +1000134}
135
136STATIC ssize_t
137xfs_file_splice_read_invis(
138 struct file *infilp,
Jens Axboecbb7e572006-04-11 14:57:50 +0200139 loff_t *ppos,
Ingo Molnar3a326a22006-04-10 15:18:35 +0200140 struct pipe_inode_info *pipe,
Nathan Scott1b895842006-03-31 13:08:59 +1000141 size_t len,
142 unsigned int flags)
143{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000144 return xfs_splice_read(XFS_I(infilp->f_path.dentry->d_inode),
145 infilp, ppos, pipe, len, flags, IO_INVIS);
Nathan Scott1b895842006-03-31 13:08:59 +1000146}
147
148STATIC ssize_t
149xfs_file_splice_write(
Ingo Molnar3a326a22006-04-10 15:18:35 +0200150 struct pipe_inode_info *pipe,
Nathan Scott1b895842006-03-31 13:08:59 +1000151 struct file *outfilp,
Jens Axboecbb7e572006-04-11 14:57:50 +0200152 loff_t *ppos,
Nathan Scott1b895842006-03-31 13:08:59 +1000153 size_t len,
154 unsigned int flags)
155{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000156 return xfs_splice_write(XFS_I(outfilp->f_path.dentry->d_inode),
157 pipe, outfilp, ppos, len, flags, 0);
Nathan Scott1b895842006-03-31 13:08:59 +1000158}
159
160STATIC ssize_t
161xfs_file_splice_write_invis(
Ingo Molnar3a326a22006-04-10 15:18:35 +0200162 struct pipe_inode_info *pipe,
Nathan Scott1b895842006-03-31 13:08:59 +1000163 struct file *outfilp,
Jens Axboecbb7e572006-04-11 14:57:50 +0200164 loff_t *ppos,
Nathan Scott1b895842006-03-31 13:08:59 +1000165 size_t len,
166 unsigned int flags)
167{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000168 return xfs_splice_write(XFS_I(outfilp->f_path.dentry->d_inode),
169 pipe, outfilp, ppos, len, flags, IO_INVIS);
Nathan Scott1b895842006-03-31 13:08:59 +1000170}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100173xfs_file_open(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 struct inode *inode,
175 struct file *filp)
176{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
178 return -EFBIG;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000179 return -xfs_open(XFS_I(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100183xfs_file_release(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct inode *inode,
185 struct file *filp)
186{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000187 return -xfs_release(XFS_I(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100191xfs_file_fsync(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 struct file *filp,
193 struct dentry *dentry,
194 int datasync)
195{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 int flags = FSYNC_WAIT;
197
198 if (datasync)
199 flags |= FSYNC_DATA;
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000200 xfs_iflags_clear(XFS_I(dentry->d_inode), XFS_ITRUNCATED);
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000201 return -xfs_fsync(XFS_I(dentry->d_inode), flags,
202 (xfs_off_t)0, (xfs_off_t)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Dean Roehrichbb3f7242005-09-02 15:43:05 +1000205#ifdef CONFIG_XFS_DMAPI
Nick Piggind0217ac2007-07-19 01:47:03 -0700206STATIC int
Nick Piggin54cb8822007-07-19 01:46:59 -0700207xfs_vm_fault(
208 struct vm_area_struct *vma,
Nick Piggind0217ac2007-07-19 01:47:03 -0700209 struct vm_fault *vmf)
Dean Roehrichbb3f7242005-09-02 15:43:05 +1000210{
Nick Piggin54cb8822007-07-19 01:46:59 -0700211 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000212 bhv_vnode_t *vp = vn_from_inode(inode);
Dean Roehrichbb3f7242005-09-02 15:43:05 +1000213
214 ASSERT_ALWAYS(vp->v_vfsp->vfs_flag & VFS_DMI);
Nick Piggind0217ac2007-07-19 01:47:03 -0700215 if (XFS_SEND_MMAP(XFS_VFSTOM(vp->v_vfsp), vma, 0))
216 return VM_FAULT_SIGBUS;
217 return filemap_fault(vma, vmf);
Dean Roehrichbb3f7242005-09-02 15:43:05 +1000218}
Dean Roehrichbb3f7242005-09-02 15:43:05 +1000219#endif /* CONFIG_XFS_DMAPI */
220
Christoph Hellwige89bc612007-12-07 14:07:53 +1100221/*
222 * Unfortunately we can't just use the clean and simple readdir implementation
223 * below, because nfs might call back into ->lookup from the filldir callback
224 * and that will deadlock the low-level btree code.
225 *
226 * Hopefully we'll find a better workaround that allows to use the optimal
227 * version at least for local readdirs for 2.6.25.
228 */
229#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100231xfs_file_readdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 struct file *filp,
233 void *dirent,
234 filldir_t filldir)
235{
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000236 struct inode *inode = filp->f_path.dentry->d_inode;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000237 xfs_inode_t *ip = XFS_I(inode);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000238 int error;
239 size_t bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000241 /*
242 * The Linux API doesn't pass down the total size of the buffer
243 * we read into down to the filesystem. With the filldir concept
244 * it's not needed for correct information, but the XFS dir2 leaf
245 * code wants an estimate of the buffer size to calculate it's
246 * readahead window and size the buffers used for mapping to
247 * physical blocks.
248 *
249 * Try to give it an estimate that's good enough, maybe at some
250 * point we can change the ->readdir prototype to include the
251 * buffer size.
252 */
253 bufsize = (size_t)min_t(loff_t, PAGE_SIZE, inode->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000255 error = xfs_readdir(ip, dirent, bufsize,
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000256 (xfs_off_t *)&filp->f_pos, filldir);
257 if (error)
258 return -error;
259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
Christoph Hellwige89bc612007-12-07 14:07:53 +1100261#else
262
263struct hack_dirent {
264 int namlen;
265 loff_t offset;
266 u64 ino;
267 unsigned int d_type;
268 char name[];
269};
270
271struct hack_callback {
272 char *dirent;
273 size_t len;
274 size_t used;
275};
276
277STATIC int
278xfs_hack_filldir(
279 void *__buf,
280 const char *name,
281 int namlen,
282 loff_t offset,
283 u64 ino,
284 unsigned int d_type)
285{
286 struct hack_callback *buf = __buf;
287 struct hack_dirent *de = (struct hack_dirent *)(buf->dirent + buf->used);
288
289 if (buf->used + sizeof(struct hack_dirent) + namlen > buf->len)
290 return -EINVAL;
291
292 de->namlen = namlen;
293 de->offset = offset;
294 de->ino = ino;
295 de->d_type = d_type;
296 memcpy(de->name, name, namlen);
297 buf->used += sizeof(struct hack_dirent) + namlen;
298 return 0;
299}
300
301STATIC int
302xfs_file_readdir(
303 struct file *filp,
304 void *dirent,
305 filldir_t filldir)
306{
307 struct inode *inode = filp->f_path.dentry->d_inode;
308 xfs_inode_t *ip = XFS_I(inode);
309 struct hack_callback buf;
310 struct hack_dirent *de;
311 int error;
312 loff_t size;
313 int eof = 0;
314 xfs_off_t start_offset, curr_offset, offset;
315
316 /*
317 * Try fairly hard to get memory
318 */
319 buf.len = PAGE_CACHE_SIZE;
320 do {
321 buf.dirent = kmalloc(buf.len, GFP_KERNEL);
322 if (buf.dirent)
323 break;
324 buf.len >>= 1;
325 } while (buf.len >= 1024);
326
327 if (!buf.dirent)
328 return -ENOMEM;
329
330 curr_offset = filp->f_pos;
331 if (curr_offset == 0x7fffffff)
332 offset = 0xffffffff;
333 else
334 offset = filp->f_pos;
335
336 while (!eof) {
337 int reclen;
338 start_offset = offset;
339
340 buf.used = 0;
341 error = -xfs_readdir(ip, &buf, buf.len, &offset,
342 xfs_hack_filldir);
343 if (error || offset == start_offset) {
344 size = 0;
345 break;
346 }
347
348 size = buf.used;
349 de = (struct hack_dirent *)buf.dirent;
350 while (size > 0) {
351 if (filldir(dirent, de->name, de->namlen,
352 curr_offset & 0x7fffffff,
353 de->ino, de->d_type)) {
354 goto done;
355 }
356
357 reclen = sizeof(struct hack_dirent) + de->namlen;
358 size -= reclen;
Christoph Hellwige89bc612007-12-07 14:07:53 +1100359 de = (struct hack_dirent *)((char *)de + reclen);
Lachlan McIlroy041388b2007-12-18 16:19:34 +1100360 curr_offset = de->offset /* & 0x7fffffff */;
Christoph Hellwige89bc612007-12-07 14:07:53 +1100361 }
362 }
363
364 done:
Lachlan McIlroy041388b2007-12-18 16:19:34 +1100365 if (!error) {
Christoph Hellwige89bc612007-12-07 14:07:53 +1100366 if (size == 0)
367 filp->f_pos = offset & 0x7fffffff;
368 else if (de)
369 filp->f_pos = curr_offset;
370 }
371
372 kfree(buf.dirent);
373 return error;
374}
375#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100378xfs_file_mmap(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 struct file *filp,
380 struct vm_area_struct *vma)
381{
Nathan Scott3562fd42006-03-14 14:00:35 +1100382 vma->vm_ops = &xfs_file_vm_ops;
Nick Piggind0217ac2007-07-19 01:47:03 -0700383 vma->vm_flags |= VM_CAN_NONLINEAR;
Dean Roehrich6fac0cb2005-06-21 14:07:45 +1000384
Dean Roehrich6fac0cb2005-06-21 14:07:45 +1000385#ifdef CONFIG_XFS_DMAPI
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000386 if (XFS_M(filp->f_path.dentry->d_inode->i_sb)->m_flags & XFS_MOUNT_DMAPI)
Nathan Scott3562fd42006-03-14 14:00:35 +1100387 vma->vm_ops = &xfs_dmapi_file_vm_ops;
Dean Roehrichbb3f7242005-09-02 15:43:05 +1000388#endif /* CONFIG_XFS_DMAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Nathan Scottfbc14622006-06-09 14:52:13 +1000390 file_accessed(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return 0;
392}
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394STATIC long
Nathan Scott3562fd42006-03-14 14:00:35 +1100395xfs_file_ioctl(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 struct file *filp,
397 unsigned int cmd,
Nathan Scott67fcaa72006-06-09 17:00:52 +1000398 unsigned long p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 int error;
Josef "Jeff" Sipeke678fb02006-12-08 02:36:49 -0800401 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000403 error = xfs_ioctl(XFS_I(inode), filp, 0, cmd, (void __user *)p);
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000404 xfs_iflags_set(XFS_I(inode), XFS_IMODIFIED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 /* NOTE: some of the ioctl's return positive #'s as a
407 * byte count indicating success, such as
408 * readlink_by_handle. So we don't "sign flip"
409 * like most other routines. This means true
410 * errors need to be returned as a negative value.
411 */
412 return error;
413}
414
415STATIC long
Nathan Scott3562fd42006-03-14 14:00:35 +1100416xfs_file_ioctl_invis(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 struct file *filp,
418 unsigned int cmd,
Nathan Scott67fcaa72006-06-09 17:00:52 +1000419 unsigned long p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Nathan Scott1b895842006-03-31 13:08:59 +1000421 int error;
Josef "Jeff" Sipeke678fb02006-12-08 02:36:49 -0800422 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000424 error = xfs_ioctl(XFS_I(inode), filp, IO_INVIS, cmd, (void __user *)p);
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000425 xfs_iflags_set(XFS_I(inode), XFS_IMODIFIED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 /* NOTE: some of the ioctl's return positive #'s as a
428 * byte count indicating success, such as
429 * readlink_by_handle. So we don't "sign flip"
430 * like most other routines. This means true
431 * errors need to be returned as a negative value.
432 */
433 return error;
434}
435
Dean Roehrichbb3f7242005-09-02 15:43:05 +1000436#ifdef CONFIG_XFS_DMAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437#ifdef HAVE_VMOP_MPROTECT
438STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100439xfs_vm_mprotect(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 struct vm_area_struct *vma,
441 unsigned int newflags)
442{
Christoph Hellwig2f6f7b32007-08-29 11:44:18 +1000443 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000444 struct xfs_mount *mp = XFS_M(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 int error = 0;
446
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000447 if (mp->m_flags & XFS_MOUNT_DMAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if ((vma->vm_flags & VM_MAYSHARE) &&
Christoph Hellwig2f6f7b32007-08-29 11:44:18 +1000449 (newflags & VM_WRITE) && !(vma->vm_flags & VM_WRITE))
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000450 error = XFS_SEND_MMAP(mp, vma, VM_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452 return error;
453}
454#endif /* HAVE_VMOP_MPROTECT */
Dean Roehrichbb3f7242005-09-02 15:43:05 +1000455#endif /* CONFIG_XFS_DMAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457#ifdef HAVE_FOP_OPEN_EXEC
458/* If the user is attempting to execute a file that is offline then
459 * we have to trigger a DMAPI READ event before the file is marked as busy
460 * otherwise the invisible I/O will not be able to write to the file to bring
461 * it back online.
462 */
463STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100464xfs_file_open_exec(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 struct inode *inode)
466{
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000467 struct xfs_mount *mp = XFS_M(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000469 if (unlikely(mp->m_flags & XFS_MOUNT_DMAPI)) {
Christoph Hellwig2f6f7b32007-08-29 11:44:18 +1000470 if (DM_EVENT_ENABLED(XFS_I(inode), DM_EVENT_READ)) {
471 bhv_vnode_t *vp = vn_from_inode(inode);
Nathan Scott1d47bec2006-06-19 08:39:16 +1000472
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000473 return -XFS_SEND_DATA(mp, DM_EVENT_READ,
Christoph Hellwig2f6f7b32007-08-29 11:44:18 +1000474 vp, 0, 0, 0, NULL);
475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
Christoph Hellwig2f6f7b32007-08-29 11:44:18 +1000477
Nathan Scott1d47bec2006-06-19 08:39:16 +1000478 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480#endif /* HAVE_FOP_OPEN_EXEC */
481
David Chinner4f57dbc2007-07-19 16:28:17 +1000482/*
483 * mmap()d file has taken write protection fault and is being made
484 * writable. We can set the page state up correctly for a writable
485 * page, which means we can do correct delalloc accounting (ENOSPC
486 * checking!) and unwritten extent mapping.
487 */
488STATIC int
489xfs_vm_page_mkwrite(
490 struct vm_area_struct *vma,
491 struct page *page)
492{
493 return block_page_mkwrite(vma, page, xfs_get_blocks);
494}
495
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800496const struct file_operations xfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 .llseek = generic_file_llseek,
498 .read = do_sync_read,
Dean Roehrichbb3f7242005-09-02 15:43:05 +1000499 .write = do_sync_write,
Nathan Scott3562fd42006-03-14 14:00:35 +1100500 .aio_read = xfs_file_aio_read,
501 .aio_write = xfs_file_aio_write,
Nathan Scott1b895842006-03-31 13:08:59 +1000502 .splice_read = xfs_file_splice_read,
503 .splice_write = xfs_file_splice_write,
Nathan Scott3562fd42006-03-14 14:00:35 +1100504 .unlocked_ioctl = xfs_file_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505#ifdef CONFIG_COMPAT
Nathan Scott3562fd42006-03-14 14:00:35 +1100506 .compat_ioctl = xfs_file_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507#endif
Nathan Scott3562fd42006-03-14 14:00:35 +1100508 .mmap = xfs_file_mmap,
509 .open = xfs_file_open,
510 .release = xfs_file_release,
511 .fsync = xfs_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512#ifdef HAVE_FOP_OPEN_EXEC
Nathan Scott3562fd42006-03-14 14:00:35 +1100513 .open_exec = xfs_file_open_exec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514#endif
515};
516
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800517const struct file_operations xfs_invis_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 .llseek = generic_file_llseek,
519 .read = do_sync_read,
Dean Roehrichbb3f7242005-09-02 15:43:05 +1000520 .write = do_sync_write,
Nathan Scott3562fd42006-03-14 14:00:35 +1100521 .aio_read = xfs_file_aio_read_invis,
522 .aio_write = xfs_file_aio_write_invis,
Nathan Scott1b895842006-03-31 13:08:59 +1000523 .splice_read = xfs_file_splice_read_invis,
524 .splice_write = xfs_file_splice_write_invis,
Nathan Scott3562fd42006-03-14 14:00:35 +1100525 .unlocked_ioctl = xfs_file_ioctl_invis,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526#ifdef CONFIG_COMPAT
Nathan Scott3562fd42006-03-14 14:00:35 +1100527 .compat_ioctl = xfs_file_compat_invis_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528#endif
Nathan Scott3562fd42006-03-14 14:00:35 +1100529 .mmap = xfs_file_mmap,
530 .open = xfs_file_open,
531 .release = xfs_file_release,
532 .fsync = xfs_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533};
534
535
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800536const struct file_operations xfs_dir_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 .read = generic_read_dir,
Nathan Scott3562fd42006-03-14 14:00:35 +1100538 .readdir = xfs_file_readdir,
539 .unlocked_ioctl = xfs_file_ioctl,
Nathan Scottd3870392005-05-06 06:44:46 -0700540#ifdef CONFIG_COMPAT
Nathan Scott3562fd42006-03-14 14:00:35 +1100541 .compat_ioctl = xfs_file_compat_ioctl,
Nathan Scottd3870392005-05-06 06:44:46 -0700542#endif
Nathan Scott3562fd42006-03-14 14:00:35 +1100543 .fsync = xfs_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544};
545
Nathan Scott3562fd42006-03-14 14:00:35 +1100546static struct vm_operations_struct xfs_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -0700547 .fault = filemap_fault,
David Chinner4f57dbc2007-07-19 16:28:17 +1000548 .page_mkwrite = xfs_vm_page_mkwrite,
Dean Roehrich6fac0cb2005-06-21 14:07:45 +1000549};
550
551#ifdef CONFIG_XFS_DMAPI
Nathan Scott3562fd42006-03-14 14:00:35 +1100552static struct vm_operations_struct xfs_dmapi_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -0700553 .fault = xfs_vm_fault,
David Chinner4f57dbc2007-07-19 16:28:17 +1000554 .page_mkwrite = xfs_vm_page_mkwrite,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555#ifdef HAVE_VMOP_MPROTECT
Nathan Scott3562fd42006-03-14 14:00:35 +1100556 .mprotect = xfs_vm_mprotect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557#endif
558};
Dean Roehrich6fac0cb2005-06-21 14:07:45 +1000559#endif /* CONFIG_XFS_DMAPI */