~ubuntu-branches/ubuntu/precise/linux-ti-omap/precise

« back to all changes in this revision

Viewing changes to ubuntu/aufs/f_op.c

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Bader, Amit Kucheria
  • Date: 2010-03-23 18:05:12 UTC
  • Revision ID: james.westby@ubuntu.com-20100323180512-iavj906ocnphdubp
Tags: 2.6.33-500.3
[ Amit Kucheria ]

* [Config] Fix the debug package name to end in -dbgsym
* SAUCE: Add the ubuntu/ drivers to omap
* SAUCE: Re-export the symbols for aufs
* [Config] Enable AUFS and COMPCACHE

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2005-2009 Junjiro R. Okajima
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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
 
17
 */
 
18
 
 
19
/*
 
20
 * file and vm operations
 
21
 */
 
22
 
 
23
#include <linux/file.h>
 
24
#include <linux/fs_stack.h>
 
25
#include <linux/ima.h>
 
26
#include <linux/mman.h>
 
27
#include <linux/mm.h>
 
28
#include <linux/security.h>
 
29
#include "aufs.h"
 
30
 
 
31
/* common function to regular file and dir */
 
32
int aufs_flush(struct file *file, fl_owner_t id)
 
33
{
 
34
        int err;
 
35
        aufs_bindex_t bindex, bend;
 
36
        struct dentry *dentry;
 
37
        struct file *h_file;
 
38
 
 
39
        dentry = file->f_dentry;
 
40
        si_noflush_read_lock(dentry->d_sb);
 
41
        fi_read_lock(file);
 
42
        di_read_lock_child(dentry, AuLock_IW);
 
43
 
 
44
        err = 0;
 
45
        bend = au_fbend(file);
 
46
        for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
 
47
                h_file = au_h_fptr(file, bindex);
 
48
                if (!h_file || !h_file->f_op || !h_file->f_op->flush)
 
49
                        continue;
 
50
 
 
51
                err = h_file->f_op->flush(h_file, id);
 
52
                if (!err)
 
53
                        vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
 
54
                /*ignore*/
 
55
        }
 
56
        au_cpup_attr_timesizes(dentry->d_inode);
 
57
 
 
58
        di_read_unlock(dentry, AuLock_IW);
 
59
        fi_read_unlock(file);
 
60
        si_read_unlock(dentry->d_sb);
 
61
        return err;
 
62
}
 
63
 
 
64
/* ---------------------------------------------------------------------- */
 
65
 
 
66
static int do_open_nondir(struct file *file, int flags)
 
67
{
 
68
        int err;
 
69
        aufs_bindex_t bindex;
 
70
        struct file *h_file;
 
71
        struct dentry *dentry;
 
72
        struct au_finfo *finfo;
 
73
 
 
74
        FiMustWriteLock(file);
 
75
 
 
76
        err = 0;
 
77
        dentry = file->f_dentry;
 
78
        finfo = au_fi(file);
 
79
        finfo->fi_h_vm_ops = NULL;
 
80
        finfo->fi_vm_ops = NULL;
 
81
        bindex = au_dbstart(dentry);
 
82
        /* O_TRUNC is processed already */
 
83
        BUG_ON(au_test_ro(dentry->d_sb, bindex, dentry->d_inode)
 
84
               && (flags & O_TRUNC));
 
85
 
 
86
        h_file = au_h_open(dentry, bindex, flags, file);
 
87
        if (IS_ERR(h_file))
 
88
                err = PTR_ERR(h_file);
 
89
        else {
 
90
                au_set_fbstart(file, bindex);
 
91
                au_set_fbend(file, bindex);
 
92
                au_set_h_fptr(file, bindex, h_file);
 
93
                au_update_figen(file);
 
94
                /* todo: necessary? */
 
95
                /* file->f_ra = h_file->f_ra; */
 
96
        }
 
97
        return err;
 
98
}
 
99
 
 
100
static int aufs_open_nondir(struct inode *inode __maybe_unused,
 
101
                            struct file *file)
 
102
{
 
103
        return au_do_open(file, do_open_nondir);
 
104
}
 
105
 
 
106
static int aufs_release_nondir(struct inode *inode __maybe_unused,
 
107
                               struct file *file)
 
108
{
 
109
        struct super_block *sb = file->f_dentry->d_sb;
 
110
 
 
111
        si_noflush_read_lock(sb);
 
112
        kfree(au_fi(file)->fi_vm_ops);
 
113
        au_finfo_fin(file);
 
114
        si_read_unlock(sb);
 
115
        return 0;
 
116
}
 
117
 
 
118
/* ---------------------------------------------------------------------- */
 
119
 
 
120
static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
 
121
                         loff_t *ppos)
 
122
{
 
123
        ssize_t err;
 
124
        struct dentry *dentry;
 
125
        struct file *h_file;
 
126
        struct super_block *sb;
 
127
 
 
128
        dentry = file->f_dentry;
 
129
        sb = dentry->d_sb;
 
130
        si_read_lock(sb, AuLock_FLUSH);
 
131
        err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
 
132
        if (unlikely(err))
 
133
                goto out;
 
134
 
 
135
        h_file = au_h_fptr(file, au_fbstart(file));
 
136
        err = vfsub_read_u(h_file, buf, count, ppos);
 
137
        /* todo: necessary? */
 
138
        /* file->f_ra = h_file->f_ra; */
 
139
        fsstack_copy_attr_atime(dentry->d_inode, h_file->f_dentry->d_inode);
 
140
 
 
141
        di_read_unlock(dentry, AuLock_IR);
 
142
        fi_read_unlock(file);
 
143
 out:
 
144
        si_read_unlock(sb);
 
145
        return err;
 
146
}
 
147
 
 
148
static ssize_t aufs_write(struct file *file, const char __user *ubuf,
 
149
                          size_t count, loff_t *ppos)
 
150
{
 
151
        ssize_t err;
 
152
        aufs_bindex_t bstart;
 
153
        struct au_pin pin;
 
154
        struct dentry *dentry;
 
155
        struct inode *inode;
 
156
        struct super_block *sb;
 
157
        struct file *h_file;
 
158
        char __user *buf = (char __user *)ubuf;
 
159
 
 
160
        dentry = file->f_dentry;
 
161
        sb = dentry->d_sb;
 
162
        inode = dentry->d_inode;
 
163
        mutex_lock(&inode->i_mutex);
 
164
        si_read_lock(sb, AuLock_FLUSH);
 
165
 
 
166
        err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
 
167
        if (unlikely(err))
 
168
                goto out;
 
169
 
 
170
        err = au_ready_to_write(file, -1, &pin);
 
171
        di_downgrade_lock(dentry, AuLock_IR);
 
172
        if (unlikely(err))
 
173
                goto out_unlock;
 
174
 
 
175
        bstart = au_fbstart(file);
 
176
        h_file = au_h_fptr(file, bstart);
 
177
        au_unpin(&pin);
 
178
        err = vfsub_write_u(h_file, buf, count, ppos);
 
179
        au_cpup_attr_timesizes(inode);
 
180
        inode->i_mode = h_file->f_dentry->d_inode->i_mode;
 
181
 
 
182
 out_unlock:
 
183
        di_read_unlock(dentry, AuLock_IR);
 
184
        fi_write_unlock(file);
 
185
 out:
 
186
        si_read_unlock(sb);
 
187
        mutex_unlock(&inode->i_mutex);
 
188
        return err;
 
189
}
 
190
 
 
191
static ssize_t aufs_aio_read(struct kiocb *kio, const struct iovec *iov,
 
192
                             unsigned long nv, loff_t pos)
 
193
{
 
194
        ssize_t err;
 
195
        struct file *file, *h_file;
 
196
        struct dentry *dentry;
 
197
        struct super_block *sb;
 
198
 
 
199
        file = kio->ki_filp;
 
200
        dentry = file->f_dentry;
 
201
        sb = dentry->d_sb;
 
202
        si_read_lock(sb, AuLock_FLUSH);
 
203
        err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
 
204
        if (unlikely(err))
 
205
                goto out;
 
206
 
 
207
        err = -ENOSYS;
 
208
        h_file = au_h_fptr(file, au_fbstart(file));
 
209
        if (h_file->f_op && h_file->f_op->aio_read) {
 
210
                err = security_file_permission(h_file, MAY_READ);
 
211
                if (unlikely(err))
 
212
                        goto out_unlock;
 
213
                if (!is_sync_kiocb(kio)) {
 
214
                        get_file(h_file);
 
215
                        fput(file);
 
216
                }
 
217
                kio->ki_filp = h_file;
 
218
                err = h_file->f_op->aio_read(kio, iov, nv, pos);
 
219
                /* todo: necessary? */
 
220
                /* file->f_ra = h_file->f_ra; */
 
221
                fsstack_copy_attr_atime(dentry->d_inode,
 
222
                                        h_file->f_dentry->d_inode);
 
223
        } else
 
224
                /* currently there is no such fs */
 
225
                WARN_ON_ONCE(h_file->f_op && h_file->f_op->read);
 
226
 
 
227
 out_unlock:
 
228
        di_read_unlock(dentry, AuLock_IR);
 
229
        fi_read_unlock(file);
 
230
 out:
 
231
        si_read_unlock(sb);
 
232
        return err;
 
233
}
 
234
 
 
235
static ssize_t aufs_aio_write(struct kiocb *kio, const struct iovec *iov,
 
236
                              unsigned long nv, loff_t pos)
 
237
{
 
238
        ssize_t err;
 
239
        aufs_bindex_t bstart;
 
240
        struct au_pin pin;
 
241
        struct dentry *dentry;
 
242
        struct inode *inode;
 
243
        struct super_block *sb;
 
244
        struct file *file, *h_file;
 
245
 
 
246
        file = kio->ki_filp;
 
247
        dentry = file->f_dentry;
 
248
        sb = dentry->d_sb;
 
249
        inode = dentry->d_inode;
 
250
        mutex_lock(&inode->i_mutex);
 
251
        si_read_lock(sb, AuLock_FLUSH);
 
252
 
 
253
        err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
 
254
        if (unlikely(err))
 
255
                goto out;
 
256
 
 
257
        err = au_ready_to_write(file, -1, &pin);
 
258
        di_downgrade_lock(dentry, AuLock_IR);
 
259
        if (unlikely(err))
 
260
                goto out_unlock;
 
261
 
 
262
        err = -ENOSYS;
 
263
        bstart = au_fbstart(file);
 
264
        h_file = au_h_fptr(file, bstart);
 
265
        au_unpin(&pin);
 
266
        if (h_file->f_op && h_file->f_op->aio_write) {
 
267
                err = security_file_permission(h_file, MAY_WRITE);
 
268
                if (unlikely(err))
 
269
                        goto out_unlock;
 
270
                if (!is_sync_kiocb(kio)) {
 
271
                        get_file(h_file);
 
272
                        fput(file);
 
273
                }
 
274
                kio->ki_filp = h_file;
 
275
                err = h_file->f_op->aio_write(kio, iov, nv, pos);
 
276
                au_cpup_attr_timesizes(inode);
 
277
                inode->i_mode = h_file->f_dentry->d_inode->i_mode;
 
278
        } else
 
279
                /* currently there is no such fs */
 
280
                WARN_ON_ONCE(h_file->f_op && h_file->f_op->write);
 
281
 
 
282
 out_unlock:
 
283
        di_read_unlock(dentry, AuLock_IR);
 
284
        fi_write_unlock(file);
 
285
 out:
 
286
        si_read_unlock(sb);
 
287
        mutex_unlock(&inode->i_mutex);
 
288
        return err;
 
289
}
 
290
 
 
291
static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
 
292
                                struct pipe_inode_info *pipe, size_t len,
 
293
                                unsigned int flags)
 
294
{
 
295
        ssize_t err;
 
296
        struct file *h_file;
 
297
        struct dentry *dentry;
 
298
        struct super_block *sb;
 
299
 
 
300
        dentry = file->f_dentry;
 
301
        sb = dentry->d_sb;
 
302
        si_read_lock(sb, AuLock_FLUSH);
 
303
        err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
 
304
        if (unlikely(err))
 
305
                goto out;
 
306
 
 
307
        err = -EINVAL;
 
308
        h_file = au_h_fptr(file, au_fbstart(file));
 
309
        if (au_test_loopback_kthread()) {
 
310
                file->f_mapping = h_file->f_mapping;
 
311
                smp_mb(); /* unnecessary? */
 
312
        }
 
313
        err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
 
314
        /* todo: necessasry? */
 
315
        /* file->f_ra = h_file->f_ra; */
 
316
        fsstack_copy_attr_atime(dentry->d_inode, h_file->f_dentry->d_inode);
 
317
 
 
318
        di_read_unlock(dentry, AuLock_IR);
 
319
        fi_read_unlock(file);
 
320
 
 
321
 out:
 
322
        si_read_unlock(sb);
 
323
        return err;
 
324
}
 
325
 
 
326
static ssize_t
 
327
aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
 
328
                  size_t len, unsigned int flags)
 
329
{
 
330
        ssize_t err;
 
331
        struct au_pin pin;
 
332
        struct dentry *dentry;
 
333
        struct inode *inode;
 
334
        struct super_block *sb;
 
335
        struct file *h_file;
 
336
 
 
337
        dentry = file->f_dentry;
 
338
        inode = dentry->d_inode;
 
339
        mutex_lock(&inode->i_mutex);
 
340
        sb = dentry->d_sb;
 
341
        si_read_lock(sb, AuLock_FLUSH);
 
342
 
 
343
        err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
 
344
        if (unlikely(err))
 
345
                goto out;
 
346
 
 
347
        err = au_ready_to_write(file, -1, &pin);
 
348
        di_downgrade_lock(dentry, AuLock_IR);
 
349
        if (unlikely(err))
 
350
                goto out_unlock;
 
351
 
 
352
        h_file = au_h_fptr(file, au_fbstart(file));
 
353
        au_unpin(&pin);
 
354
        err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
 
355
        au_cpup_attr_timesizes(inode);
 
356
        inode->i_mode = h_file->f_dentry->d_inode->i_mode;
 
357
 
 
358
 out_unlock:
 
359
        di_read_unlock(dentry, AuLock_IR);
 
360
        fi_write_unlock(file);
 
361
 out:
 
362
        si_read_unlock(sb);
 
363
        mutex_unlock(&inode->i_mutex);
 
364
        return err;
 
365
}
 
366
 
 
367
/* ---------------------------------------------------------------------- */
 
368
 
 
369
static struct file *au_safe_file(struct vm_area_struct *vma)
 
370
{
 
371
        struct file *file;
 
372
 
 
373
        file = vma->vm_file;
 
374
        if (file->private_data && au_test_aufs(file->f_dentry->d_sb))
 
375
                return file;
 
376
        return NULL;
 
377
}
 
378
 
 
379
static void au_reset_file(struct vm_area_struct *vma, struct file *file)
 
380
{
 
381
        vma->vm_file = file;
 
382
        /* smp_mb(); */ /* flush vm_file */
 
383
}
 
384
 
 
385
static int aufs_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
 
386
{
 
387
        int err;
 
388
        static DECLARE_WAIT_QUEUE_HEAD(wq);
 
389
        struct file *file, *h_file;
 
390
        struct au_finfo *finfo;
 
391
 
 
392
        /* todo: non-robr mode, user vm_file as it is? */
 
393
        wait_event(wq, (file = au_safe_file(vma)));
 
394
 
 
395
        /* do not revalidate, no si lock */
 
396
        finfo = au_fi(file);
 
397
        h_file = finfo->fi_hfile[0 + finfo->fi_bstart].hf_file;
 
398
        AuDebugOn(!h_file || !finfo->fi_h_vm_ops);
 
399
 
 
400
        mutex_lock(&finfo->fi_vm_mtx);
 
401
        vma->vm_file = h_file;
 
402
        err = finfo->fi_h_vm_ops->fault(vma, vmf);
 
403
        /* todo: necessary? */
 
404
        /* file->f_ra = h_file->f_ra; */
 
405
        au_reset_file(vma, file);
 
406
        mutex_unlock(&finfo->fi_vm_mtx);
 
407
#if 0 /* def CONFIG_SMP */
 
408
        /* wake_up_nr(&wq, online_cpu - 1); */
 
409
        wake_up_all(&wq);
 
410
#else
 
411
        wake_up(&wq);
 
412
#endif
 
413
 
 
414
        return err;
 
415
}
 
416
 
 
417
static int aufs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
 
418
{
 
419
        int err;
 
420
        static DECLARE_WAIT_QUEUE_HEAD(wq);
 
421
        struct file *file, *h_file;
 
422
        struct au_finfo *finfo;
 
423
 
 
424
        wait_event(wq, (file = au_safe_file(vma)));
 
425
 
 
426
        finfo = au_fi(file);
 
427
        h_file = finfo->fi_hfile[0 + finfo->fi_bstart].hf_file;
 
428
        AuDebugOn(!h_file || !finfo->fi_h_vm_ops);
 
429
 
 
430
        mutex_lock(&finfo->fi_vm_mtx);
 
431
        vma->vm_file = h_file;
 
432
        err = finfo->fi_h_vm_ops->page_mkwrite(vma, vmf);
 
433
        au_reset_file(vma, file);
 
434
        mutex_unlock(&finfo->fi_vm_mtx);
 
435
        wake_up(&wq);
 
436
 
 
437
        return err;
 
438
}
 
439
 
 
440
static void aufs_vm_close(struct vm_area_struct *vma)
 
441
{
 
442
        static DECLARE_WAIT_QUEUE_HEAD(wq);
 
443
        struct file *file, *h_file;
 
444
        struct au_finfo *finfo;
 
445
 
 
446
        wait_event(wq, (file = au_safe_file(vma)));
 
447
 
 
448
        finfo = au_fi(file);
 
449
        h_file = finfo->fi_hfile[0 + finfo->fi_bstart].hf_file;
 
450
        AuDebugOn(!h_file || !finfo->fi_h_vm_ops);
 
451
 
 
452
        mutex_lock(&finfo->fi_vm_mtx);
 
453
        vma->vm_file = h_file;
 
454
        finfo->fi_h_vm_ops->close(vma);
 
455
        au_reset_file(vma, file);
 
456
        mutex_unlock(&finfo->fi_vm_mtx);
 
457
        wake_up(&wq);
 
458
}
 
459
 
 
460
static struct vm_operations_struct aufs_vm_ops = {
 
461
        /* .close and .page_mkwrite are not set by default */
 
462
        .fault          = aufs_fault,
 
463
};
 
464
 
 
465
/* ---------------------------------------------------------------------- */
 
466
 
 
467
static unsigned long au_prot_conv(unsigned long flags)
 
468
{
 
469
        unsigned long prot;
 
470
 
 
471
        prot = 0;
 
472
        if (flags & VM_READ)
 
473
                prot |= PROT_READ;
 
474
        if (flags & VM_WRITE)
 
475
                prot |= PROT_WRITE;
 
476
        if (flags & VM_EXEC)
 
477
                prot |= PROT_EXEC;
 
478
        return prot;
 
479
}
 
480
 
 
481
static struct vm_operations_struct *au_vm_ops(struct file *h_file,
 
482
                                              struct vm_area_struct *vma)
 
483
{
 
484
        struct vm_operations_struct *vm_ops;
 
485
        int err;
 
486
 
 
487
        vm_ops = ERR_PTR(-ENODEV);
 
488
        if (!h_file->f_op || !h_file->f_op->mmap)
 
489
                goto out;
 
490
 
 
491
        err = ima_file_mmap(h_file, au_prot_conv(vma->vm_flags));
 
492
        vm_ops = ERR_PTR(err);
 
493
        if (err)
 
494
                goto out;
 
495
 
 
496
        err = h_file->f_op->mmap(h_file, vma);
 
497
        vm_ops = ERR_PTR(err);
 
498
        if (unlikely(err))
 
499
                goto out;
 
500
 
 
501
        /* oops, it became 'const' */
 
502
        vm_ops = (struct vm_operations_struct *)vma->vm_ops;
 
503
        err = do_munmap(current->mm, vma->vm_start,
 
504
                        vma->vm_end - vma->vm_start);
 
505
        if (unlikely(err)) {
 
506
                AuIOErr("failed internal unmapping %.*s, %d\n",
 
507
                        AuDLNPair(h_file->f_dentry), err);
 
508
                vm_ops = ERR_PTR(-EIO);
 
509
        }
 
510
 
 
511
 out:
 
512
        return vm_ops;
 
513
}
 
514
 
 
515
static int au_custom_vm_ops(struct au_finfo *finfo, struct vm_area_struct *vma)
 
516
{
 
517
        int err;
 
518
        struct vm_operations_struct *h_ops;
 
519
 
 
520
        AuRwMustAnyLock(&finfo->fi_rwsem);
 
521
 
 
522
        err = 0;
 
523
        h_ops = finfo->fi_h_vm_ops;
 
524
        AuDebugOn(!h_ops);
 
525
        if ((!h_ops->page_mkwrite && !h_ops->close)
 
526
            || finfo->fi_vm_ops)
 
527
                goto out;
 
528
 
 
529
        err = -ENOMEM;
 
530
        finfo->fi_vm_ops = kmemdup(&aufs_vm_ops, sizeof(aufs_vm_ops), GFP_NOFS);
 
531
        if (unlikely(!finfo->fi_vm_ops))
 
532
                goto out;
 
533
 
 
534
        err = 0;
 
535
        if (h_ops->page_mkwrite)
 
536
                finfo->fi_vm_ops->page_mkwrite = aufs_page_mkwrite;
 
537
        if (h_ops->close)
 
538
                finfo->fi_vm_ops->close = aufs_vm_close;
 
539
 
 
540
        vma->vm_ops = finfo->fi_vm_ops;
 
541
 
 
542
 out:
 
543
        return err;
 
544
}
 
545
 
 
546
static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
 
547
{
 
548
        int err;
 
549
        unsigned char wlock, mmapped;
 
550
        struct dentry *dentry;
 
551
        struct super_block *sb;
 
552
        struct file *h_file;
 
553
        struct vm_operations_struct *vm_ops;
 
554
 
 
555
        dentry = file->f_dentry;
 
556
        wlock = !!(file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
 
557
        sb = dentry->d_sb;
 
558
        si_read_lock(sb, AuLock_FLUSH);
 
559
        err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
 
560
        if (unlikely(err))
 
561
                goto out;
 
562
 
 
563
        mmapped = !!au_test_mmapped(file);
 
564
        if (wlock) {
 
565
                struct au_pin pin;
 
566
 
 
567
                err = au_ready_to_write(file, -1, &pin);
 
568
                di_downgrade_lock(dentry, AuLock_IR);
 
569
                if (unlikely(err))
 
570
                        goto out_unlock;
 
571
                au_unpin(&pin);
 
572
        } else
 
573
                di_downgrade_lock(dentry, AuLock_IR);
 
574
 
 
575
        h_file = au_h_fptr(file, au_fbstart(file));
 
576
        if (!mmapped && au_test_fs_bad_mapping(h_file->f_dentry->d_sb)) {
 
577
                /*
 
578
                 * by this assignment, f_mapping will differs from aufs inode
 
579
                 * i_mapping.
 
580
                 * if someone else mixes the use of f_dentry->d_inode and
 
581
                 * f_mapping->host, then a problem may arise.
 
582
                 */
 
583
                file->f_mapping = h_file->f_mapping;
 
584
        }
 
585
 
 
586
        vm_ops = NULL;
 
587
        if (!mmapped) {
 
588
                vm_ops = au_vm_ops(h_file, vma);
 
589
                err = PTR_ERR(vm_ops);
 
590
                if (IS_ERR(vm_ops))
 
591
                        goto out_unlock;
 
592
        }
 
593
 
 
594
        /*
 
595
         * unnecessary to handle MAP_DENYWRITE and deny_write_access()?
 
596
         * currently MAP_DENYWRITE from userspace is ignored, but elf loader
 
597
         * sets it. when FMODE_EXEC is set (by open_exec() or sys_uselib()),
 
598
         * both of the aufs file and the lower file is deny_write_access()-ed.
 
599
         * finally I hope we can skip handlling MAP_DENYWRITE here.
 
600
         */
 
601
        err = generic_file_mmap(file, vma);
 
602
        if (unlikely(err))
 
603
                goto out_unlock;
 
604
 
 
605
        vma->vm_ops = &aufs_vm_ops;
 
606
        if (!mmapped) {
 
607
                struct au_finfo *finfo = au_fi(file);
 
608
 
 
609
                finfo->fi_h_vm_ops = vm_ops;
 
610
                mutex_init(&finfo->fi_vm_mtx);
 
611
        }
 
612
 
 
613
        err = au_custom_vm_ops(au_fi(file), vma);
 
614
        if (unlikely(err))
 
615
                goto out_unlock;
 
616
 
 
617
        vfsub_file_accessed(h_file);
 
618
        fsstack_copy_attr_atime(dentry->d_inode, h_file->f_dentry->d_inode);
 
619
 
 
620
 out_unlock:
 
621
        di_read_unlock(dentry, AuLock_IR);
 
622
        fi_write_unlock(file);
 
623
 out:
 
624
        si_read_unlock(sb);
 
625
        return err;
 
626
}
 
627
 
 
628
/* ---------------------------------------------------------------------- */
 
629
 
 
630
static int aufs_fsync_nondir(struct file *file, struct dentry *dentry,
 
631
                             int datasync)
 
632
{
 
633
        int err;
 
634
        struct au_pin pin;
 
635
        struct inode *inode;
 
636
        struct file *h_file;
 
637
        struct super_block *sb;
 
638
 
 
639
        inode = dentry->d_inode;
 
640
        IMustLock(file->f_mapping->host);
 
641
        if (inode != file->f_mapping->host) {
 
642
                mutex_unlock(&file->f_mapping->host->i_mutex);
 
643
                mutex_lock(&inode->i_mutex);
 
644
        }
 
645
        IMustLock(inode);
 
646
 
 
647
        sb = dentry->d_sb;
 
648
        si_read_lock(sb, AuLock_FLUSH);
 
649
 
 
650
        err = 0; /* -EBADF; */ /* posix? */
 
651
        if (unlikely(!(file->f_mode & FMODE_WRITE)))
 
652
                goto out;
 
653
        err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
 
654
        if (unlikely(err))
 
655
                goto out;
 
656
 
 
657
        err = au_ready_to_write(file, -1, &pin);
 
658
        di_downgrade_lock(dentry, AuLock_IR);
 
659
        if (unlikely(err))
 
660
                goto out_unlock;
 
661
        au_unpin(&pin);
 
662
 
 
663
        err = -EINVAL;
 
664
        h_file = au_h_fptr(file, au_fbstart(file));
 
665
        if (h_file->f_op && h_file->f_op->fsync) {
 
666
                struct dentry *h_d;
 
667
                struct mutex *h_mtx;
 
668
 
 
669
                /*
 
670
                 * no filemap_fdatawrite() since aufs file has no its own
 
671
                 * mapping, but dir.
 
672
                 */
 
673
                h_d = h_file->f_dentry;
 
674
                h_mtx = &h_d->d_inode->i_mutex;
 
675
                mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
 
676
                err = h_file->f_op->fsync(h_file, h_d, datasync);
 
677
                if (!err)
 
678
                        vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
 
679
                /*ignore*/
 
680
                au_cpup_attr_timesizes(inode);
 
681
                mutex_unlock(h_mtx);
 
682
        }
 
683
 
 
684
 out_unlock:
 
685
        di_read_unlock(dentry, AuLock_IR);
 
686
        fi_write_unlock(file);
 
687
 out:
 
688
        si_read_unlock(sb);
 
689
        if (inode != file->f_mapping->host) {
 
690
                mutex_unlock(&inode->i_mutex);
 
691
                mutex_lock(&file->f_mapping->host->i_mutex);
 
692
        }
 
693
        return err;
 
694
}
 
695
 
 
696
/* no one supports this operation, currently */
 
697
#if 0
 
698
static int aufs_aio_fsync_nondir(struct kiocb *kio, int datasync)
 
699
{
 
700
        int err;
 
701
        struct au_pin pin;
 
702
        struct dentry *dentry;
 
703
        struct inode *inode;
 
704
        struct file *file, *h_file;
 
705
        struct super_block *sb;
 
706
 
 
707
        file = kio->ki_filp;
 
708
        dentry = file->f_dentry;
 
709
        inode = dentry->d_inode;
 
710
        mutex_lock(&inode->i_mutex);
 
711
 
 
712
        sb = dentry->d_sb;
 
713
        si_read_lock(sb, AuLock_FLUSH);
 
714
 
 
715
        err = 0; /* -EBADF; */ /* posix? */
 
716
        if (unlikely(!(file->f_mode & FMODE_WRITE)))
 
717
                goto out;
 
718
        err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
 
719
        if (unlikely(err))
 
720
                goto out;
 
721
 
 
722
        err = au_ready_to_write(file, -1, &pin);
 
723
        di_downgrade_lock(dentry, AuLock_IR);
 
724
        if (unlikely(err))
 
725
                goto out_unlock;
 
726
        au_unpin(&pin);
 
727
 
 
728
        err = -ENOSYS;
 
729
        h_file = au_h_fptr(file, au_fbstart(file));
 
730
        if (h_file->f_op && h_file->f_op->aio_fsync) {
 
731
                struct dentry *h_d;
 
732
                struct mutex *h_mtx;
 
733
 
 
734
                h_d = h_file->f_dentry;
 
735
                h_mtx = &h_d->d_inode->i_mutex;
 
736
                if (!is_sync_kiocb(kio)) {
 
737
                        get_file(h_file);
 
738
                        fput(file);
 
739
                }
 
740
                kio->ki_filp = h_file;
 
741
                err = h_file->f_op->aio_fsync(kio, datasync);
 
742
                mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
 
743
                if (!err)
 
744
                        vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
 
745
                /*ignore*/
 
746
                au_cpup_attr_timesizes(inode);
 
747
                mutex_unlock(h_mtx);
 
748
        }
 
749
 
 
750
 out_unlock:
 
751
        di_read_unlock(dentry, AuLock_IR);
 
752
        fi_write_unlock(file);
 
753
 out:
 
754
        si_read_unlock(sb);
 
755
        mutex_unlock(&inode->i_mutex);
 
756
        return err;
 
757
}
 
758
#endif
 
759
 
 
760
static int aufs_fasync(int fd, struct file *file, int flag)
 
761
{
 
762
        int err;
 
763
        struct file *h_file;
 
764
        struct dentry *dentry;
 
765
        struct super_block *sb;
 
766
 
 
767
        dentry = file->f_dentry;
 
768
        sb = dentry->d_sb;
 
769
        si_read_lock(sb, AuLock_FLUSH);
 
770
        err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
 
771
        if (unlikely(err))
 
772
                goto out;
 
773
 
 
774
        h_file = au_h_fptr(file, au_fbstart(file));
 
775
        if (h_file->f_op && h_file->f_op->fasync)
 
776
                err = h_file->f_op->fasync(fd, h_file, flag);
 
777
 
 
778
        di_read_unlock(dentry, AuLock_IR);
 
779
        fi_read_unlock(file);
 
780
 
 
781
 out:
 
782
        si_read_unlock(sb);
 
783
        return err;
 
784
}
 
785
 
 
786
/* ---------------------------------------------------------------------- */
 
787
 
 
788
/* no one supports this operation, currently */
 
789
#if 0
 
790
static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
 
791
                             size_t len, loff_t *pos , int more)
 
792
{
 
793
}
 
794
#endif
 
795
 
 
796
/* ---------------------------------------------------------------------- */
 
797
 
 
798
const struct file_operations aufs_file_fop = {
 
799
        /*
 
800
         * while generic_file_llseek/_unlocked() don't use BKL,
 
801
         * don't use it since it operates file->f_mapping->host.
 
802
         * in aufs, it may be a real file and may confuse users by UDBA.
 
803
         */
 
804
        /* .llseek              = generic_file_llseek, */
 
805
 
 
806
        .read           = aufs_read,
 
807
        .write          = aufs_write,
 
808
        .aio_read       = aufs_aio_read,
 
809
        .aio_write      = aufs_aio_write,
 
810
#ifdef CONFIG_AUFS_POLL
 
811
        .poll           = aufs_poll,
 
812
#endif
 
813
        .unlocked_ioctl = aufs_ioctl_nondir,
 
814
        .mmap           = aufs_mmap,
 
815
        .open           = aufs_open_nondir,
 
816
        .flush          = aufs_flush,
 
817
        .release        = aufs_release_nondir,
 
818
        .fsync          = aufs_fsync_nondir,
 
819
        /* .aio_fsync   = aufs_aio_fsync_nondir, */
 
820
        .fasync         = aufs_fasync,
 
821
        /* .sendpage    = aufs_sendpage, */
 
822
        .splice_write   = aufs_splice_write,
 
823
        .splice_read    = aufs_splice_read,
 
824
#if 0
 
825
        .aio_splice_write = aufs_aio_splice_write,
 
826
        .aio_splice_read  = aufs_aio_splice_read
 
827
#endif
 
828
};