2
* Copyright (C) 2005-2008 Junjiro Okajima
4
* This program, aufs is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will 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.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
* file and vm operations
22
* $Id: f_op.c,v 1.3 2008/04/28 03:04:12 sfjro Exp $
25
//#include <linux/fsnotify.h>
26
#include <linux/fs_stack.h>
27
//#include <linux/pagemap.h>
28
#include <linux/poll.h>
29
//#include <linux/security.h>
32
/* common function to regular file and dir */
33
int aufs_flush(struct file *file, fl_owner_t id)
36
struct dentry *dentry;
37
aufs_bindex_t bindex, bend;
39
dentry = file->f_dentry;
40
LKTRTrace("%.*s\n", AuDLNPair(dentry));
42
// aufs_read_lock_file()
43
si_read_lock(dentry->d_sb, !AuLock_FLUSH);
45
di_read_lock_child(dentry, AuLock_IW);
48
bend = au_fbend(file);
49
for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
51
h_file = au_h_fptr(file, bindex);
52
if (h_file && h_file->f_op && h_file->f_op->flush) {
53
err = h_file->f_op->flush(h_file, id);
55
au_update_fuse_h_inode
56
(h_file->f_vfsmnt, h_file->f_dentry);
60
au_cpup_attr_timesizes(dentry->d_inode);
62
di_read_unlock(dentry, AuLock_IW);
64
si_read_unlock(dentry->d_sb);
69
/* ---------------------------------------------------------------------- */
71
static int do_open_nondir(struct file *file, int flags)
75
struct super_block *sb;
77
struct dentry *dentry;
79
struct au_finfo *finfo;
81
dentry = file->f_dentry;
82
LKTRTrace("%.*s, flags 0%o\n", AuDLNPair(dentry), flags);
83
FiMustWriteLock(file);
84
inode = dentry->d_inode;
85
AuDebugOn(!inode || S_ISDIR(inode->i_mode));
89
finfo->fi_h_vm_ops = NULL;
91
bindex = au_dbstart(dentry);
92
AuDebugOn(!au_h_dptr(dentry, bindex)->d_inode);
93
/* O_TRUNC is processed already */
94
BUG_ON(au_test_ro(sb, bindex, inode) && (flags & O_TRUNC));
96
h_file = au_h_open(dentry, bindex, flags, file);
97
//if (LktrCond) {fput(h_file); au_br_put(au_sbr(dentry->d_sb, bindex));
98
//h_file = ERR_PTR(-1);}
99
if (!IS_ERR(h_file)) {
100
au_set_fbstart(file, bindex);
101
au_set_fbend(file, bindex);
102
au_set_h_fptr(file, bindex, h_file);
103
au_update_figen(file);
104
//file->f_ra = h_file->f_ra; //??
106
return 0; /* success */
108
err = PTR_ERR(h_file);
113
static int aufs_open_nondir(struct inode *inode, struct file *file)
115
return au_do_open(inode, file, do_open_nondir);
118
static int aufs_release_nondir(struct inode *inode, struct file *file)
120
struct super_block *sb = file->f_dentry->d_sb;
122
LKTRTrace("i%lu, %.*s\n", inode->i_ino, AuDLNPair(file->f_dentry));
124
si_read_lock(sb, !AuLock_FLUSH);
130
/* ---------------------------------------------------------------------- */
132
static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
136
struct dentry *dentry;
138
struct super_block *sb;
139
struct inode *h_inode;
141
dentry = file->f_dentry;
142
LKTRTrace("%.*s, cnt %lu, pos %Ld\n",
143
AuDLNPair(dentry), (unsigned long)count, *ppos);
144
//AuDbgDentry(dentry);
147
si_read_lock(sb, AuLock_FLUSH);
148
err = au_reval_and_lock_finfo(file, au_reopen_nondir, /*wlock*/0,
150
//if (LktrCond) {fi_read_unlock(file); err = -1;}
154
/* support LSM and notify */
155
h_file = au_h_fptr(file, au_fbstart(file));
156
h_inode = h_file->f_dentry->d_inode;
157
err = vfsub_read_u(h_file, buf, count, ppos,
158
au_opt_test_dlgt(au_mntflags(sb)));
159
//file->f_ra = h_file->f_ra; //??
160
fsstack_copy_attr_atime(dentry->d_inode, h_file->f_dentry->d_inode);
162
fi_read_unlock(file);
169
static ssize_t aufs_write(struct file *file, const char __user *ubuf,
170
size_t count, loff_t *ppos)
173
struct dentry *dentry, *parent;
174
struct inode *inode, *dir;
175
struct super_block *sb;
176
unsigned int mnt_flags;
178
char __user *buf = (char __user *)ubuf;
179
struct au_hin_ignore ign;
180
struct vfsub_args vargs;
182
dentry = file->f_dentry;
183
LKTRTrace("%.*s, cnt %lu, pos %Ld\n",
184
AuDLNPair(dentry), (unsigned long)count, *ppos);
186
inode = dentry->d_inode;
187
mutex_lock(&inode->i_mutex);
189
si_read_lock(sb, AuLock_FLUSH);
190
err = au_reval_and_lock_finfo(file, au_reopen_nondir, /*wlock*/1,
192
//if (LktrCond) {fi_write_unlock(file); err = -1;}
195
err = au_ready_to_write(file, -1);
196
//if (LktrCond) err = -1;
200
/* support LSM and notify */
201
mnt_flags = au_mntflags(sb);
202
vfsub_args_init(&vargs, &ign, au_opt_test_dlgt(mnt_flags), 0);
203
h_file = au_h_fptr(file, au_fbstart(file));
204
if (!au_opt_test(mnt_flags, UDBA_INOTIFY))
205
err = vfsub_write_u(h_file, buf, count, ppos, &vargs);
207
parent = dget_parent(dentry);
208
dir = parent->d_inode;
209
ii_read_lock_parent(dir);
210
vfsub_ign_hinode(&vargs, IN_MODIFY,
211
au_hi(dir, au_fbstart(file)));
212
err = vfsub_write_u(h_file, buf, count, ppos, &vargs);
216
ii_write_lock_child(inode);
217
au_cpup_attr_timesizes(inode);
218
ii_write_unlock(inode);
221
fi_write_unlock(file);
224
mutex_unlock(&inode->i_mutex);
229
#ifdef CONFIG_AUFS_SPLICE_PATCH
230
static int au_test_loopback(void)
232
const char c = current->comm[4];
233
/* true if a kernel thread named 'loop[0-9].*' accesses a file */
234
const int loopback = (current->mm == NULL
235
&& '0' <= c && c <= '9'
236
&& strncmp(current->comm, "loop", 4) == 0);
240
static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
241
struct pipe_inode_info *pipe, size_t len,
246
struct dentry *dentry;
247
struct super_block *sb;
249
dentry = file->f_dentry;
250
LKTRTrace("%.*s, pos %Ld, len %lu\n",
251
AuDLNPair(dentry), *ppos, (unsigned long)len);
254
si_read_lock(sb, AuLock_FLUSH);
255
err = au_reval_and_lock_finfo(file, au_reopen_nondir, /*wlock*/0,
261
/* support LSM and notify */
262
h_file = au_h_fptr(file, au_fbstart(file));
263
if (/* unlikely */(au_test_loopback())) {
264
file->f_mapping = h_file->f_mapping;
265
smp_mb(); /* unnecessary? */
267
err = vfsub_splice_to(h_file, ppos, pipe, len, flags,
268
au_opt_test_dlgt(au_mntflags(sb)));
269
//file->f_ra = h_file->f_ra; //??
270
fsstack_copy_attr_atime(dentry->d_inode, h_file->f_dentry->d_inode);
271
fi_read_unlock(file);
280
aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
281
size_t len, unsigned int flags)
284
struct dentry *dentry;
285
struct inode *inode, *h_inode;
286
struct super_block *sb;
288
struct au_hin_ignore ign;
289
struct vfsub_args vargs;
290
unsigned int mnt_flags;
292
dentry = file->f_dentry;
293
LKTRTrace("%.*s, len %lu, pos %Ld\n",
294
AuDLNPair(dentry), (unsigned long)len, *ppos);
296
inode = dentry->d_inode;
297
mutex_lock(&inode->i_mutex);
299
si_read_lock(sb, AuLock_FLUSH);
300
err = au_reval_and_lock_finfo(file, au_reopen_nondir, /*wlock*/1,
302
//if (LktrCond) {fi_write_unlock(file); err = -1;}
305
err = au_ready_to_write(file, -1);
306
//if (LktrCond) err = -1;
310
/* support LSM and notify */
311
mnt_flags = au_mntflags(sb);
312
vfsub_args_init(&vargs, &ign, au_opt_test_dlgt(mnt_flags), 0);
313
h_file = au_h_fptr(file, au_fbstart(file));
314
h_inode = h_file->f_dentry->d_inode;
315
/* current do_splice_from() doesn't fire up the inotify event */
316
if (1 || !au_opt_test(mnt_flags, UDBA_INOTIFY))
317
err = vfsub_splice_from(pipe, h_file, ppos, len, flags, &vargs);
319
//struct dentry *parent = dget_parent(dentry);
320
//vfsub_ign_hinode(&vargs, IN_MODIFY,
321
//au_hi(parent->d_inode, au_fbstart(file));
322
err = vfsub_splice_from(pipe, h_file, ppos, len, flags, &vargs);
325
ii_write_lock_child(inode);
326
au_cpup_attr_timesizes(inode);
327
ii_write_unlock(inode);
330
fi_write_unlock(file);
333
mutex_unlock(&inode->i_mutex);
337
#endif /* CONFIG_AUFS_SPLICE_PATCH */
339
/* ---------------------------------------------------------------------- */
341
static int aufs_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
344
struct dentry *dentry;
345
struct file *file, *h_file;
347
static DECLARE_WAIT_QUEUE_HEAD(wq);
348
struct au_finfo *finfo;
352
AuDebugOn(!vma || !vma->vm_file);
353
//todo: non-robr mode, user vm_file as it is.
354
wait_event(wq, (file = au_robr_safe_file(vma)));
355
AuDebugOn(!au_test_aufs(file->f_dentry->d_sb));
356
dentry = file->f_dentry;
357
LKTRTrace("%.*s\n", AuDLNPair(dentry));
358
inode = dentry->d_inode;
359
AuDebugOn(!S_ISREG(inode->i_mode));
361
/* do not revalidate, nor lock */
363
h_file = finfo->fi_hfile[0 + finfo->fi_bstart].hf_file;
364
AuDebugOn(!h_file || !au_test_mmapped(file));
365
vma->vm_file = h_file;
367
err = finfo->fi_h_vm_ops->fault(vma, vmf);
368
//file->f_ra = h_file->f_ra; //??
369
au_robr_reset_file(vma, file);
370
#if 0 //def CONFIG_SMP
371
//wake_up_nr(&wq, online_cpu - 1);
377
if (!(err & VM_FAULT_ERROR)) {
380
AuDbg("%p, %d\n", page, page_mapcount(page));
382
//page->mapping = file->f_mapping;
384
//file->f_mapping = h_file->f_mapping;
385
//touch_atime(NULL, dentry);
386
//inode->i_atime = h_file->f_dentry->d_inode->i_atime;
389
//AuDbg("err %d\n", err);
393
static struct vm_operations_struct aufs_vm_ops = {
394
//.open = aufs_vmaopen,
395
//.close = aufs_vmaclose,
398
unsigned long (*nopfn)(struct vm_area_struct *area,
399
unsigned long address);
400
//page_mkwrite(struct vm_area_struct *vma, struct page *page)
404
/* ---------------------------------------------------------------------- */
406
static struct vm_operations_struct *au_vm_ops(struct file *h_file,
407
struct vm_area_struct *vma)
409
struct vm_operations_struct *vm_ops;
414
if (!au_test_nfs(h_file->f_vfsmnt->mnt_sb))
415
err = h_file->f_op->mmap(h_file, vma);
418
err = h_file->f_op->mmap(h_file, vma);
421
vm_ops = ERR_PTR(err);
424
vm_ops = vma->vm_ops;
425
err = do_munmap(current->mm, vma->vm_start,
426
vma->vm_end - vma->vm_start);
428
AuIOErr("failed internal unmapping %.*s, %d\n",
429
AuDLNPair(h_file->f_dentry), err);
430
vm_ops = ERR_PTR(-EIO);
434
AuTraceErrPtr(vm_ops);
438
static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
440
int err, wlock, mmapped;
441
struct dentry *dentry;
442
struct super_block *sb;
444
struct vm_operations_struct *vm_ops;
446
dentry = file->f_dentry;
447
LKTRTrace("%.*s, %lx, len %lu\n",
448
AuDLNPair(dentry), vma->vm_start,
449
vma->vm_end - vma->vm_start);
450
AuDebugOn(!S_ISREG(dentry->d_inode->i_mode));
451
AuDebugOn(down_write_trylock(&vma->vm_mm->mmap_sem));
453
mmapped = au_test_mmapped(file); /* can be harmless race condition */
454
wlock = !!(file->f_mode & FMODE_WRITE);
456
si_read_lock(sb, AuLock_FLUSH);
457
err = au_reval_and_lock_finfo(file, au_reopen_nondir,
458
wlock | !mmapped, /*locked*/0);
464
err = au_ready_to_write(file, -1);
470
h_file = au_h_fptr(file, au_fbstart(file));
471
if (unlikely(au_test_fuse(h_file->f_dentry->d_sb))) {
473
* by this assignment, f_mapping will differs from aufs inode
475
* if someone else mixes the use of f_dentry->d_inode and
476
* f_mapping->host, then a problem may arise.
478
file->f_mapping = h_file->f_mapping;
481
if (0 && h_file->f_op->mmap == generic_file_mmap) {
482
err = generic_file_mmap(file, vma); /* instead of h_file */
485
au_fi(file)->fi_h_vm_ops = vma->vm_ops;
489
vm_ops = au_vm_ops(h_file, vma);
490
err = PTR_ERR(vm_ops);
495
err = generic_file_mmap(file, vma);
498
vma->vm_ops = &aufs_vm_ops;
500
au_fi(file)->fi_h_vm_ops = vm_ops;
503
file_accessed(h_file);
504
au_update_fuse_h_inode(h_file->f_vfsmnt, h_file->f_dentry); /*ignore*/
505
fsstack_copy_attr_atime(dentry->d_inode, h_file->f_dentry->d_inode);
508
if (!wlock && mmapped)
509
fi_read_unlock(file);
511
fi_write_unlock(file);
518
/* ---------------------------------------------------------------------- */
520
static unsigned int aufs_poll(struct file *file, poll_table *wait)
525
struct dentry *dentry;
526
struct super_block *sb;
528
dentry = file->f_dentry;
529
LKTRTrace("%.*s, wait %p\n", AuDLNPair(dentry), wait);
530
AuDebugOn(S_ISDIR(dentry->d_inode->i_mode));
532
/* We should pretend an error happened. */
533
mask = POLLERR /* | POLLIN | POLLOUT */;
535
si_read_lock(sb, AuLock_FLUSH);
536
err = au_reval_and_lock_finfo(file, au_reopen_nondir, /*wlock*/0,
542
/* it is not an error of hidden_file has no operation */
543
mask = DEFAULT_POLLMASK;
544
h_file = au_h_fptr(file, au_fbstart(file));
545
if (h_file->f_op && h_file->f_op->poll)
546
mask = h_file->f_op->poll(h_file, wait);
547
fi_read_unlock(file);
551
AuTraceErr((int)mask);
555
static int aufs_fsync_nondir(struct file *file, struct dentry *dentry,
561
struct super_block *sb;
563
LKTRTrace("%.*s, %d\n", AuDLNPair(dentry), datasync);
564
inode = dentry->d_inode;
566
IMustLock(file->f_mapping->host);
567
if (unlikely(inode != file->f_mapping->host)) {
568
mutex_unlock(&file->f_mapping->host->i_mutex);
569
mutex_lock(&inode->i_mutex);
574
si_read_lock(sb, AuLock_FLUSH);
575
err = 0; //-EBADF; // posix?
576
if (unlikely(!(file->f_mode & FMODE_WRITE)))
578
err = au_reval_and_lock_finfo(file, au_reopen_nondir, /*wlock*/1,
583
err = au_ready_to_write(file, -1);
589
h_file = au_h_fptr(file, au_fbstart(file));
590
if (h_file->f_op && h_file->f_op->fsync) {
591
ii_write_lock_child(inode);
592
mutex_lock_nested(&h_file->f_dentry->d_inode->i_mutex,
594
err = h_file->f_op->fsync(h_file, h_file->f_dentry, datasync);
597
au_update_fuse_h_inode(h_file->f_vfsmnt,
599
au_cpup_attr_timesizes(inode);
600
mutex_unlock(&h_file->f_dentry->d_inode->i_mutex);
601
ii_write_unlock(inode);
605
fi_write_unlock(file);
608
if (unlikely(inode != file->f_mapping->host)) {
609
mutex_unlock(&inode->i_mutex);
610
mutex_lock(&file->f_mapping->host->i_mutex);
616
static int aufs_fasync(int fd, struct file *file, int flag)
620
struct dentry *dentry;
621
struct super_block *sb;
623
dentry = file->f_dentry;
624
LKTRTrace("%.*s, %d\n", AuDLNPair(dentry), flag);
627
si_read_lock(sb, AuLock_FLUSH);
628
err = au_reval_and_lock_finfo(file, au_reopen_nondir, /*wlock*/0,
634
h_file = au_h_fptr(file, au_fbstart(file));
635
if (h_file->f_op && h_file->f_op->fasync)
636
err = h_file->f_op->fasync(fd, h_file, flag);
637
fi_read_unlock(file);
645
/* ---------------------------------------------------------------------- */
647
struct file_operations aufs_file_fop = {
652
.open = aufs_open_nondir,
654
.release = aufs_release_nondir,
655
.fsync = aufs_fsync_nondir,
656
.fasync = aufs_fasync,
657
#ifdef CONFIG_AUFS_SPLICE_PATCH
658
.splice_write = aufs_splice_write,
659
.splice_read = aufs_splice_read,