0b9aa487f012c9160f91b90fa9f630707218b328
[pandora-kernel.git] / fs / aufs / file.c
1 /*
2  * Copyright (C) 2005-2012 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  * handling file/dir, and address_space operation
21  */
22
23 #include <linux/pagemap.h>
24 #include "aufs.h"
25
26 /* drop flags for writing */
27 unsigned int au_file_roflags(unsigned int flags)
28 {
29         flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
30         flags |= O_RDONLY | O_NOATIME;
31         return flags;
32 }
33
34 /* common functions to regular file and dir */
35 struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
36                        struct file *file)
37 {
38         struct file *h_file;
39         struct dentry *h_dentry;
40         struct inode *h_inode;
41         struct super_block *sb;
42         struct au_branch *br;
43         struct path h_path;
44         int err, exec_flag;
45
46         /* a race condition can happen between open and unlink/rmdir */
47         h_file = ERR_PTR(-ENOENT);
48         h_dentry = au_h_dptr(dentry, bindex);
49         if (au_test_nfsd() && !h_dentry)
50                 goto out;
51         h_inode = h_dentry->d_inode;
52         if (au_test_nfsd() && !h_inode)
53                 goto out;
54         spin_lock(&h_dentry->d_lock);
55         err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
56                 || !h_inode
57                 /* || !dentry->d_inode->i_nlink */
58                 ;
59         spin_unlock(&h_dentry->d_lock);
60         if (unlikely(err))
61                 goto out;
62
63         sb = dentry->d_sb;
64         br = au_sbr(sb, bindex);
65         h_file = ERR_PTR(-EACCES);
66         exec_flag = flags & __FMODE_EXEC;
67         if (exec_flag && (br->br_mnt->mnt_flags & MNT_NOEXEC))
68                 goto out;
69
70         /* drop flags for writing */
71         if (au_test_ro(sb, bindex, dentry->d_inode))
72                 flags = au_file_roflags(flags);
73         flags &= ~O_CREAT;
74         atomic_inc(&br->br_count);
75         h_path.dentry = h_dentry;
76         h_path.mnt = br->br_mnt;
77         if (!au_special_file(h_inode->i_mode))
78                 h_file = vfsub_dentry_open(&h_path, flags);
79         else {
80                 /* this block depends upon the configuration */
81                 di_read_unlock(dentry, AuLock_IR);
82                 fi_write_unlock(file);
83                 si_read_unlock(sb);
84                 h_file = vfsub_dentry_open(&h_path, flags);
85                 si_noflush_read_lock(sb);
86                 fi_write_lock(file);
87                 di_read_lock_child(dentry, AuLock_IR);
88         }
89         if (IS_ERR(h_file))
90                 goto out_br;
91
92         if (exec_flag) {
93                 err = deny_write_access(h_file);
94                 if (unlikely(err)) {
95                         fput(h_file);
96                         h_file = ERR_PTR(err);
97                         goto out_br;
98                 }
99         }
100         fsnotify_open(h_file);
101         goto out; /* success */
102
103 out_br:
104         atomic_dec(&br->br_count);
105 out:
106         return h_file;
107 }
108
109 int au_do_open(struct file *file, int (*open)(struct file *file, int flags),
110                struct au_fidir *fidir)
111 {
112         int err;
113         struct dentry *dentry;
114
115         err = au_finfo_init(file, fidir);
116         if (unlikely(err))
117                 goto out;
118
119         dentry = file->f_dentry;
120         di_read_lock_child(dentry, AuLock_IR);
121         err = open(file, vfsub_file_flags(file));
122         di_read_unlock(dentry, AuLock_IR);
123
124         fi_write_unlock(file);
125         if (unlikely(err)) {
126                 au_fi(file)->fi_hdir = NULL;
127                 au_finfo_fin(file);
128         }
129
130 out:
131         return err;
132 }
133
134 int au_reopen_nondir(struct file *file)
135 {
136         int err;
137         aufs_bindex_t bstart;
138         struct dentry *dentry;
139         struct file *h_file, *h_file_tmp;
140
141         dentry = file->f_dentry;
142         AuDebugOn(au_special_file(dentry->d_inode->i_mode));
143         bstart = au_dbstart(dentry);
144         h_file_tmp = NULL;
145         if (au_fbstart(file) == bstart) {
146                 h_file = au_hf_top(file);
147                 if (file->f_mode == h_file->f_mode)
148                         return 0; /* success */
149                 h_file_tmp = h_file;
150                 get_file(h_file_tmp);
151                 au_set_h_fptr(file, bstart, NULL);
152         }
153         AuDebugOn(au_fi(file)->fi_hdir);
154         AuDebugOn(au_fbstart(file) < bstart);
155
156         h_file = au_h_open(dentry, bstart, vfsub_file_flags(file) & ~O_TRUNC,
157                            file);
158         err = PTR_ERR(h_file);
159         if (IS_ERR(h_file))
160                 goto out; /* todo: close all? */
161
162         err = 0;
163         au_set_fbstart(file, bstart);
164         au_set_h_fptr(file, bstart, h_file);
165         au_update_figen(file);
166         /* todo: necessary? */
167         /* file->f_ra = h_file->f_ra; */
168
169 out:
170         if (h_file_tmp)
171                 fput(h_file_tmp);
172         return err;
173 }
174
175 /* ---------------------------------------------------------------------- */
176
177 static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
178                         struct dentry *hi_wh)
179 {
180         int err;
181         aufs_bindex_t bstart;
182         struct au_dinfo *dinfo;
183         struct dentry *h_dentry;
184         struct au_hdentry *hdp;
185
186         dinfo = au_di(file->f_dentry);
187         AuRwMustWriteLock(&dinfo->di_rwsem);
188
189         bstart = dinfo->di_bstart;
190         dinfo->di_bstart = btgt;
191         hdp = dinfo->di_hdentry;
192         h_dentry = hdp[0 + btgt].hd_dentry;
193         hdp[0 + btgt].hd_dentry = hi_wh;
194         err = au_reopen_nondir(file);
195         hdp[0 + btgt].hd_dentry = h_dentry;
196         dinfo->di_bstart = bstart;
197
198         return err;
199 }
200
201 static int au_ready_to_write_wh(struct file *file, loff_t len,
202                                 aufs_bindex_t bcpup)
203 {
204         int err;
205         struct inode *inode, *h_inode;
206         struct dentry *dentry, *h_dentry, *hi_wh;
207
208         dentry = file->f_dentry;
209         au_update_dbstart(dentry);
210         inode = dentry->d_inode;
211         h_inode = NULL;
212         if (au_dbstart(dentry) <= bcpup && au_dbend(dentry) >= bcpup) {
213                 h_dentry = au_h_dptr(dentry, bcpup);
214                 if (h_dentry)
215                         h_inode = h_dentry->d_inode;
216         }
217         hi_wh = au_hi_wh(inode, bcpup);
218         if (!hi_wh && !h_inode)
219                 err = au_sio_cpup_wh(dentry, bcpup, len, file);
220         else
221                 /* already copied-up after unlink */
222                 err = au_reopen_wh(file, bcpup, hi_wh);
223
224         if (!err
225             && inode->i_nlink > 1
226             && au_opt_test(au_mntflags(dentry->d_sb), PLINK))
227                 au_plink_append(inode, bcpup, au_h_dptr(dentry, bcpup));
228
229         return err;
230 }
231
232 /*
233  * prepare the @file for writing.
234  */
235 int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
236 {
237         int err;
238         aufs_bindex_t bstart, bcpup, dbstart;
239         struct dentry *dentry, *parent, *h_dentry;
240         struct inode *h_inode, *inode;
241         struct super_block *sb;
242         struct file *h_file;
243
244         dentry = file->f_dentry;
245         sb = dentry->d_sb;
246         inode = dentry->d_inode;
247         AuDebugOn(au_special_file(inode->i_mode));
248         bstart = au_fbstart(file);
249         err = au_test_ro(sb, bstart, inode);
250         if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
251                 err = au_pin(pin, dentry, bstart, AuOpt_UDBA_NONE, /*flags*/0);
252                 goto out;
253         }
254
255         /* need to cpup or reopen */
256         parent = dget_parent(dentry);
257         di_write_lock_parent(parent);
258         err = AuWbrCopyup(au_sbi(sb), dentry);
259         bcpup = err;
260         if (unlikely(err < 0))
261                 goto out_dgrade;
262         err = 0;
263
264         if (!d_unhashed(dentry) && !au_h_dptr(parent, bcpup)) {
265                 err = au_cpup_dirs(dentry, bcpup);
266                 if (unlikely(err))
267                         goto out_dgrade;
268         }
269
270         err = au_pin(pin, dentry, bcpup, AuOpt_UDBA_NONE,
271                      AuPin_DI_LOCKED | AuPin_MNT_WRITE);
272         if (unlikely(err))
273                 goto out_dgrade;
274
275         h_dentry = au_hf_top(file)->f_dentry;
276         h_inode = h_dentry->d_inode;
277         dbstart = au_dbstart(dentry);
278         if (dbstart <= bcpup) {
279                 h_dentry = au_h_dptr(dentry, bcpup);
280                 AuDebugOn(!h_dentry);
281                 h_inode = h_dentry->d_inode;
282                 AuDebugOn(!h_inode);
283                 bstart = bcpup;
284         }
285
286         if (dbstart <= bcpup            /* just reopen */
287             || !d_unhashed(dentry)      /* copyup and reopen */
288                 ) {
289                 mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
290                 h_file = au_h_open_pre(dentry, bstart);
291                 if (IS_ERR(h_file)) {
292                         err = PTR_ERR(h_file);
293                         h_file = NULL;
294                 } else {
295                         di_downgrade_lock(parent, AuLock_IR);
296                         if (dbstart > bcpup)
297                                 err = au_sio_cpup_simple(dentry, bcpup, len,
298                                                          AuCpup_DTIME);
299                         if (!err)
300                                 err = au_reopen_nondir(file);
301                 }
302                 mutex_unlock(&h_inode->i_mutex);
303                 au_h_open_post(dentry, bstart, h_file);
304         } else {                        /* copyup as wh and reopen */
305                 /*
306                  * since writable hfsplus branch is not supported,
307                  * h_open_pre/post() are unnecessary.
308                  */
309                 mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
310                 err = au_ready_to_write_wh(file, len, bcpup);
311                 di_downgrade_lock(parent, AuLock_IR);
312                 mutex_unlock(&h_inode->i_mutex);
313         }
314
315         if (!err) {
316                 au_pin_set_parent_lflag(pin, /*lflag*/0);
317                 goto out_dput; /* success */
318         }
319         au_unpin(pin);
320         goto out_unlock;
321
322 out_dgrade:
323         di_downgrade_lock(parent, AuLock_IR);
324 out_unlock:
325         di_read_unlock(parent, AuLock_IR);
326 out_dput:
327         dput(parent);
328 out:
329         return err;
330 }
331
332 /* ---------------------------------------------------------------------- */
333
334 int au_do_flush(struct file *file, fl_owner_t id,
335                 int (*flush)(struct file *file, fl_owner_t id))
336 {
337         int err;
338         struct dentry *dentry;
339         struct super_block *sb;
340         struct inode *inode;
341
342         dentry = file->f_dentry;
343         sb = dentry->d_sb;
344         inode = dentry->d_inode;
345         si_noflush_read_lock(sb);
346         fi_read_lock(file);
347         ii_read_lock_child(inode);
348
349         err = flush(file, id);
350         au_cpup_attr_timesizes(inode);
351
352         ii_read_unlock(inode);
353         fi_read_unlock(file);
354         si_read_unlock(sb);
355         return err;
356 }
357
358 /* ---------------------------------------------------------------------- */
359
360 static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
361 {
362         int err;
363         aufs_bindex_t bstart;
364         struct au_pin pin;
365         struct au_finfo *finfo;
366         struct dentry *dentry, *parent, *hi_wh;
367         struct inode *inode;
368         struct super_block *sb;
369
370         FiMustWriteLock(file);
371
372         err = 0;
373         finfo = au_fi(file);
374         dentry = file->f_dentry;
375         sb = dentry->d_sb;
376         inode = dentry->d_inode;
377         bstart = au_ibstart(inode);
378         if (bstart == finfo->fi_btop || IS_ROOT(dentry))
379                 goto out;
380
381         parent = dget_parent(dentry);
382         if (au_test_ro(sb, bstart, inode)) {
383                 di_read_lock_parent(parent, !AuLock_IR);
384                 err = AuWbrCopyup(au_sbi(sb), dentry);
385                 bstart = err;
386                 di_read_unlock(parent, !AuLock_IR);
387                 if (unlikely(err < 0))
388                         goto out_parent;
389                 err = 0;
390         }
391
392         di_read_lock_parent(parent, AuLock_IR);
393         hi_wh = au_hi_wh(inode, bstart);
394         if (!S_ISDIR(inode->i_mode)
395             && au_opt_test(au_mntflags(sb), PLINK)
396             && au_plink_test(inode)
397             && !d_unhashed(dentry)) {
398                 err = au_test_and_cpup_dirs(dentry, bstart);
399                 if (unlikely(err))
400                         goto out_unlock;
401
402                 /* always superio. */
403                 err = au_pin(&pin, dentry, bstart, AuOpt_UDBA_NONE,
404                              AuPin_DI_LOCKED | AuPin_MNT_WRITE);
405                 if (!err)
406                         err = au_sio_cpup_simple(dentry, bstart, -1,
407                                                  AuCpup_DTIME);
408                 au_unpin(&pin);
409         } else if (hi_wh) {
410                 /* already copied-up after unlink */
411                 err = au_reopen_wh(file, bstart, hi_wh);
412                 *need_reopen = 0;
413         }
414
415 out_unlock:
416         di_read_unlock(parent, AuLock_IR);
417 out_parent:
418         dput(parent);
419 out:
420         return err;
421 }
422
423 static void au_do_refresh_dir(struct file *file)
424 {
425         aufs_bindex_t bindex, bend, new_bindex, brid;
426         struct au_hfile *p, tmp, *q;
427         struct au_finfo *finfo;
428         struct super_block *sb;
429         struct au_fidir *fidir;
430
431         FiMustWriteLock(file);
432
433         sb = file->f_dentry->d_sb;
434         finfo = au_fi(file);
435         fidir = finfo->fi_hdir;
436         AuDebugOn(!fidir);
437         p = fidir->fd_hfile + finfo->fi_btop;
438         brid = p->hf_br->br_id;
439         bend = fidir->fd_bbot;
440         for (bindex = finfo->fi_btop; bindex <= bend; bindex++, p++) {
441                 if (!p->hf_file)
442                         continue;
443
444                 new_bindex = au_br_index(sb, p->hf_br->br_id);
445                 if (new_bindex == bindex)
446                         continue;
447                 if (new_bindex < 0) {
448                         au_set_h_fptr(file, bindex, NULL);
449                         continue;
450                 }
451
452                 /* swap two lower inode, and loop again */
453                 q = fidir->fd_hfile + new_bindex;
454                 tmp = *q;
455                 *q = *p;
456                 *p = tmp;
457                 if (tmp.hf_file) {
458                         bindex--;
459                         p--;
460                 }
461         }
462
463         p = fidir->fd_hfile;
464         if (!au_test_mmapped(file) && !d_unlinked(file->f_dentry)) {
465                 bend = au_sbend(sb);
466                 for (finfo->fi_btop = 0; finfo->fi_btop <= bend;
467                      finfo->fi_btop++, p++)
468                         if (p->hf_file) {
469                                 if (p->hf_file->f_dentry
470                                     && p->hf_file->f_dentry->d_inode)
471                                         break;
472                                 else
473                                         au_hfput(p, file);
474                         }
475         } else {
476                 bend = au_br_index(sb, brid);
477                 for (finfo->fi_btop = 0; finfo->fi_btop < bend;
478                      finfo->fi_btop++, p++)
479                         if (p->hf_file)
480                                 au_hfput(p, file);
481                 bend = au_sbend(sb);
482         }
483
484         p = fidir->fd_hfile + bend;
485         for (fidir->fd_bbot = bend; fidir->fd_bbot >= finfo->fi_btop;
486              fidir->fd_bbot--, p--)
487                 if (p->hf_file) {
488                         if (p->hf_file->f_dentry
489                             && p->hf_file->f_dentry->d_inode)
490                                 break;
491                         else
492                                 au_hfput(p, file);
493                 }
494         AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
495 }
496
497 /*
498  * after branch manipulating, refresh the file.
499  */
500 static int refresh_file(struct file *file, int (*reopen)(struct file *file))
501 {
502         int err, need_reopen;
503         aufs_bindex_t bend, bindex;
504         struct dentry *dentry;
505         struct au_finfo *finfo;
506         struct au_hfile *hfile;
507
508         dentry = file->f_dentry;
509         finfo = au_fi(file);
510         if (!finfo->fi_hdir) {
511                 hfile = &finfo->fi_htop;
512                 AuDebugOn(!hfile->hf_file);
513                 bindex = au_br_index(dentry->d_sb, hfile->hf_br->br_id);
514                 AuDebugOn(bindex < 0);
515                 if (bindex != finfo->fi_btop)
516                         au_set_fbstart(file, bindex);
517         } else {
518                 err = au_fidir_realloc(finfo, au_sbend(dentry->d_sb) + 1);
519                 if (unlikely(err))
520                         goto out;
521                 au_do_refresh_dir(file);
522         }
523
524         err = 0;
525         need_reopen = 1;
526         if (!au_test_mmapped(file))
527                 err = au_file_refresh_by_inode(file, &need_reopen);
528         if (!err && need_reopen && !d_unlinked(dentry))
529                 err = reopen(file);
530         if (!err) {
531                 au_update_figen(file);
532                 goto out; /* success */
533         }
534
535         /* error, close all lower files */
536         if (finfo->fi_hdir) {
537                 bend = au_fbend_dir(file);
538                 for (bindex = au_fbstart(file); bindex <= bend; bindex++)
539                         au_set_h_fptr(file, bindex, NULL);
540         }
541
542 out:
543         return err;
544 }
545
546 /* common function to regular file and dir */
547 int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
548                           int wlock)
549 {
550         int err;
551         unsigned int sigen, figen;
552         aufs_bindex_t bstart;
553         unsigned char pseudo_link;
554         struct dentry *dentry;
555         struct inode *inode;
556
557         err = 0;
558         dentry = file->f_dentry;
559         inode = dentry->d_inode;
560         AuDebugOn(au_special_file(inode->i_mode));
561         sigen = au_sigen(dentry->d_sb);
562         fi_write_lock(file);
563         figen = au_figen(file);
564         di_write_lock_child(dentry);
565         bstart = au_dbstart(dentry);
566         pseudo_link = (bstart != au_ibstart(inode));
567         if (sigen == figen && !pseudo_link && au_fbstart(file) == bstart) {
568                 if (!wlock) {
569                         di_downgrade_lock(dentry, AuLock_IR);
570                         fi_downgrade_lock(file);
571                 }
572                 goto out; /* success */
573         }
574
575         AuDbg("sigen %d, figen %d\n", sigen, figen);
576         if (au_digen_test(dentry, sigen)) {
577                 err = au_reval_dpath(dentry, sigen);
578                 AuDebugOn(!err && au_digen_test(dentry, sigen));
579         }
580
581         if (!err)
582                 err = refresh_file(file, reopen);
583         if (!err) {
584                 if (!wlock) {
585                         di_downgrade_lock(dentry, AuLock_IR);
586                         fi_downgrade_lock(file);
587                 }
588         } else {
589                 di_write_unlock(dentry);
590                 fi_write_unlock(file);
591         }
592
593 out:
594         return err;
595 }
596
597 /* ---------------------------------------------------------------------- */
598
599 /* cf. aufs_nopage() */
600 /* for madvise(2) */
601 static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
602 {
603         unlock_page(page);
604         return 0;
605 }
606
607 /* it will never be called, but necessary to support O_DIRECT */
608 static ssize_t aufs_direct_IO(int rw, struct kiocb *iocb,
609                               const struct iovec *iov, loff_t offset,
610                               unsigned long nr_segs)
611 { BUG(); return 0; }
612
613 /*
614  * it will never be called, but madvise and fadvise behaves differently
615  * when get_xip_mem is defined
616  */
617 static int aufs_get_xip_mem(struct address_space *mapping, pgoff_t pgoff,
618                             int create, void **kmem, unsigned long *pfn)
619 { BUG(); return 0; }
620
621 /* they will never be called. */
622 #ifdef CONFIG_AUFS_DEBUG
623 static int aufs_write_begin(struct file *file, struct address_space *mapping,
624                             loff_t pos, unsigned len, unsigned flags,
625                             struct page **pagep, void **fsdata)
626 { AuUnsupport(); return 0; }
627 static int aufs_write_end(struct file *file, struct address_space *mapping,
628                           loff_t pos, unsigned len, unsigned copied,
629                           struct page *page, void *fsdata)
630 { AuUnsupport(); return 0; }
631 static int aufs_writepage(struct page *page, struct writeback_control *wbc)
632 { AuUnsupport(); return 0; }
633
634 static int aufs_set_page_dirty(struct page *page)
635 { AuUnsupport(); return 0; }
636 static void aufs_invalidatepage(struct page *page, unsigned long offset)
637 { AuUnsupport(); }
638 static int aufs_releasepage(struct page *page, gfp_t gfp)
639 { AuUnsupport(); return 0; }
640 static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
641                             struct page *page)
642 { AuUnsupport(); return 0; }
643 static int aufs_launder_page(struct page *page)
644 { AuUnsupport(); return 0; }
645 static int aufs_is_partially_uptodate(struct page *page,
646                                       read_descriptor_t *desc,
647                                       unsigned long from)
648 { AuUnsupport(); return 0; }
649 static int aufs_error_remove_page(struct address_space *mapping,
650                                   struct page *page)
651 { AuUnsupport(); return 0; }
652 #endif /* CONFIG_AUFS_DEBUG */
653
654 const struct address_space_operations aufs_aop = {
655         .readpage               = aufs_readpage,
656         .direct_IO              = aufs_direct_IO,
657         .get_xip_mem            = aufs_get_xip_mem,
658 #ifdef CONFIG_AUFS_DEBUG
659         .writepage              = aufs_writepage,
660         /* no writepages, because of writepage */
661         .set_page_dirty         = aufs_set_page_dirty,
662         /* no readpages, because of readpage */
663         .write_begin            = aufs_write_begin,
664         .write_end              = aufs_write_end,
665         /* no bmap, no block device */
666         .invalidatepage         = aufs_invalidatepage,
667         .releasepage            = aufs_releasepage,
668         .migratepage            = aufs_migratepage,
669         .launder_page           = aufs_launder_page,
670         .is_partially_uptodate  = aufs_is_partially_uptodate,
671         .error_remove_page      = aufs_error_remove_page
672 #endif /* CONFIG_AUFS_DEBUG */
673 };