2 * Copyright (C) 2005-2013 Junjiro R. 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 * handling file/dir, and address_space operation
23 #ifdef CONFIG_AUFS_DEBUG
24 #include <linux/migrate.h>
26 #include <linux/pagemap.h>
29 /* drop flags for writing */
30 unsigned int au_file_roflags(unsigned int flags)
32 flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
33 flags |= O_RDONLY | O_NOATIME;
37 /* common functions to regular file and dir */
38 struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
42 struct dentry *h_dentry;
43 struct inode *h_inode;
44 struct super_block *sb;
49 /* a race condition can happen between open and unlink/rmdir */
50 h_file = ERR_PTR(-ENOENT);
51 h_dentry = au_h_dptr(dentry, bindex);
52 if (au_test_nfsd() && !h_dentry)
54 h_inode = h_dentry->d_inode;
55 if (au_test_nfsd() && !h_inode)
57 spin_lock(&h_dentry->d_lock);
58 err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
60 /* || !dentry->d_inode->i_nlink */
62 spin_unlock(&h_dentry->d_lock);
67 br = au_sbr(sb, bindex);
68 h_file = ERR_PTR(-EACCES);
69 exec_flag = flags & __FMODE_EXEC;
70 if (exec_flag && (au_br_mnt(br)->mnt_flags & MNT_NOEXEC))
73 /* drop flags for writing */
74 if (au_test_ro(sb, bindex, dentry->d_inode))
75 flags = au_file_roflags(flags);
77 atomic_inc(&br->br_count);
78 h_path.dentry = h_dentry;
79 h_path.mnt = au_br_mnt(br);
80 if (!au_special_file(h_inode->i_mode))
81 h_file = vfsub_dentry_open(&h_path, flags);
83 /* this block depends upon the configuration */
84 di_read_unlock(dentry, AuLock_IR);
85 fi_write_unlock(file);
87 h_file = vfsub_dentry_open(&h_path, flags);
88 si_noflush_read_lock(sb);
90 di_read_lock_child(dentry, AuLock_IR);
96 err = deny_write_access(h_file);
99 h_file = ERR_PTR(err);
103 fsnotify_open(h_file);
104 goto out; /* success */
107 atomic_dec(&br->br_count);
112 int au_do_open(struct file *file, int (*open)(struct file *file, int flags),
113 struct au_fidir *fidir)
116 struct dentry *dentry;
118 err = au_finfo_init(file, fidir);
122 dentry = file->f_dentry;
123 di_read_lock_child(dentry, AuLock_IR);
124 err = open(file, vfsub_file_flags(file));
125 di_read_unlock(dentry, AuLock_IR);
127 fi_write_unlock(file);
129 au_fi(file)->fi_hdir = NULL;
137 int au_reopen_nondir(struct file *file)
140 aufs_bindex_t bstart;
141 struct dentry *dentry;
142 struct file *h_file, *h_file_tmp;
144 dentry = file->f_dentry;
145 AuDebugOn(au_special_file(dentry->d_inode->i_mode));
146 bstart = au_dbstart(dentry);
148 if (au_fbstart(file) == bstart) {
149 h_file = au_hf_top(file);
150 if (file->f_mode == h_file->f_mode)
151 return 0; /* success */
153 get_file(h_file_tmp);
154 au_set_h_fptr(file, bstart, NULL);
156 AuDebugOn(au_fi(file)->fi_hdir);
159 * file exists on both of rw and ro
160 * open --> dbstart and fbstart are both 0
161 * prepend a branch as rw, "rw" become ro
163 * delete the top branch, "rw" becomes rw again
164 * --> dbstart is 1, fbstart is still 0
165 * write --> fbstart is 0 but dbstart is 1
167 /* AuDebugOn(au_fbstart(file) < bstart); */
169 h_file = au_h_open(dentry, bstart, vfsub_file_flags(file) & ~O_TRUNC,
171 err = PTR_ERR(h_file);
172 if (IS_ERR(h_file)) {
174 atomic_inc(&au_sbr(dentry->d_sb, bstart)->br_count);
175 au_set_h_fptr(file, bstart, h_file_tmp);
178 goto out; /* todo: close all? */
182 au_set_fbstart(file, bstart);
183 au_set_h_fptr(file, bstart, h_file);
184 au_update_figen(file);
185 /* todo: necessary? */
186 /* file->f_ra = h_file->f_ra; */
194 /* ---------------------------------------------------------------------- */
196 static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
197 struct dentry *hi_wh)
200 aufs_bindex_t bstart;
201 struct au_dinfo *dinfo;
202 struct dentry *h_dentry;
203 struct au_hdentry *hdp;
205 dinfo = au_di(file->f_dentry);
206 AuRwMustWriteLock(&dinfo->di_rwsem);
208 bstart = dinfo->di_bstart;
209 dinfo->di_bstart = btgt;
210 hdp = dinfo->di_hdentry;
211 h_dentry = hdp[0 + btgt].hd_dentry;
212 hdp[0 + btgt].hd_dentry = hi_wh;
213 err = au_reopen_nondir(file);
214 hdp[0 + btgt].hd_dentry = h_dentry;
215 dinfo->di_bstart = bstart;
220 static int au_ready_to_write_wh(struct file *file, loff_t len,
221 aufs_bindex_t bcpup, struct au_pin *pin)
224 struct inode *inode, *h_inode;
225 struct dentry *h_dentry, *hi_wh;
226 struct au_cp_generic cpg = {
227 .dentry = file->f_dentry,
234 au_update_dbstart(cpg.dentry);
235 inode = cpg.dentry->d_inode;
237 if (au_dbstart(cpg.dentry) <= bcpup
238 && au_dbend(cpg.dentry) >= bcpup) {
239 h_dentry = au_h_dptr(cpg.dentry, bcpup);
241 h_inode = h_dentry->d_inode;
243 hi_wh = au_hi_wh(inode, bcpup);
244 if (!hi_wh && !h_inode)
245 err = au_sio_cpup_wh(&cpg, file);
247 /* already copied-up after unlink */
248 err = au_reopen_wh(file, bcpup, hi_wh);
251 && inode->i_nlink > 1
252 && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
253 au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
259 * prepare the @file for writing.
261 int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
264 aufs_bindex_t dbstart;
265 struct dentry *parent, *h_dentry;
267 struct super_block *sb;
269 struct au_cp_generic cpg = {
270 .dentry = file->f_dentry,
275 .flags = AuCpup_DTIME
278 sb = cpg.dentry->d_sb;
279 inode = cpg.dentry->d_inode;
280 AuDebugOn(au_special_file(inode->i_mode));
281 cpg.bsrc = au_fbstart(file);
282 err = au_test_ro(sb, cpg.bsrc, inode);
283 if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
284 err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
289 /* need to cpup or reopen */
290 parent = dget_parent(cpg.dentry);
291 di_write_lock_parent(parent);
292 err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
294 if (unlikely(err < 0))
298 if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
299 err = au_cpup_dirs(cpg.dentry, cpg.bdst);
304 err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
305 AuPin_DI_LOCKED | AuPin_MNT_WRITE);
309 h_dentry = au_hf_top(file)->f_dentry;
310 dbstart = au_dbstart(cpg.dentry);
311 if (dbstart <= cpg.bdst) {
312 h_dentry = au_h_dptr(cpg.dentry, cpg.bdst);
313 AuDebugOn(!h_dentry);
317 if (dbstart <= cpg.bdst /* just reopen */
318 || !d_unhashed(cpg.dentry) /* copyup and reopen */
320 h_file = au_h_open_pre(cpg.dentry, cpg.bsrc);
322 err = PTR_ERR(h_file);
324 di_downgrade_lock(parent, AuLock_IR);
325 if (dbstart > cpg.bdst)
326 err = au_sio_cpup_simple(&cpg);
328 err = au_reopen_nondir(file);
329 au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
331 } else { /* copyup as wh and reopen */
333 * since writable hfsplus branch is not supported,
334 * h_open_pre/post() are unnecessary.
336 err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
337 di_downgrade_lock(parent, AuLock_IR);
341 au_pin_set_parent_lflag(pin, /*lflag*/0);
342 goto out_dput; /* success */
348 di_downgrade_lock(parent, AuLock_IR);
350 di_read_unlock(parent, AuLock_IR);
357 /* ---------------------------------------------------------------------- */
359 int au_do_flush(struct file *file, fl_owner_t id,
360 int (*flush)(struct file *file, fl_owner_t id))
363 struct dentry *dentry;
364 struct super_block *sb;
367 dentry = file->f_dentry;
369 inode = dentry->d_inode;
370 si_noflush_read_lock(sb);
372 ii_read_lock_child(inode);
374 err = flush(file, id);
375 au_cpup_attr_timesizes(inode);
377 ii_read_unlock(inode);
378 fi_read_unlock(file);
383 /* ---------------------------------------------------------------------- */
385 static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
389 struct au_finfo *finfo;
390 struct dentry *parent, *hi_wh;
392 struct super_block *sb;
393 struct au_cp_generic cpg = {
394 .dentry = file->f_dentry,
399 .flags = AuCpup_DTIME
402 FiMustWriteLock(file);
406 sb = cpg.dentry->d_sb;
407 inode = cpg.dentry->d_inode;
408 cpg.bdst = au_ibstart(inode);
409 if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
412 parent = dget_parent(cpg.dentry);
413 if (au_test_ro(sb, cpg.bdst, inode)) {
414 di_read_lock_parent(parent, !AuLock_IR);
415 err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
417 di_read_unlock(parent, !AuLock_IR);
418 if (unlikely(err < 0))
423 di_read_lock_parent(parent, AuLock_IR);
424 hi_wh = au_hi_wh(inode, cpg.bdst);
425 if (!S_ISDIR(inode->i_mode)
426 && au_opt_test(au_mntflags(sb), PLINK)
427 && au_plink_test(inode)
428 && !d_unhashed(cpg.dentry)
429 && cpg.bdst < au_dbstart(cpg.dentry)) {
430 err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
434 /* always superio. */
435 err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
436 AuPin_DI_LOCKED | AuPin_MNT_WRITE);
438 err = au_sio_cpup_simple(&cpg);
442 /* already copied-up after unlink */
443 err = au_reopen_wh(file, cpg.bdst, hi_wh);
448 di_read_unlock(parent, AuLock_IR);
455 static void au_do_refresh_dir(struct file *file)
457 aufs_bindex_t bindex, bend, new_bindex, brid;
458 struct au_hfile *p, tmp, *q;
459 struct au_finfo *finfo;
460 struct super_block *sb;
461 struct au_fidir *fidir;
463 FiMustWriteLock(file);
465 sb = file->f_dentry->d_sb;
467 fidir = finfo->fi_hdir;
469 p = fidir->fd_hfile + finfo->fi_btop;
470 brid = p->hf_br->br_id;
471 bend = fidir->fd_bbot;
472 for (bindex = finfo->fi_btop; bindex <= bend; bindex++, p++) {
476 new_bindex = au_br_index(sb, p->hf_br->br_id);
477 if (new_bindex == bindex)
479 if (new_bindex < 0) {
480 au_set_h_fptr(file, bindex, NULL);
484 /* swap two lower inode, and loop again */
485 q = fidir->fd_hfile + new_bindex;
496 if (!au_test_mmapped(file) && !d_unlinked(file->f_dentry)) {
498 for (finfo->fi_btop = 0; finfo->fi_btop <= bend;
499 finfo->fi_btop++, p++)
501 if (p->hf_file->f_dentry
502 && p->hf_file->f_dentry->d_inode)
508 bend = au_br_index(sb, brid);
509 for (finfo->fi_btop = 0; finfo->fi_btop < bend;
510 finfo->fi_btop++, p++)
516 p = fidir->fd_hfile + bend;
517 for (fidir->fd_bbot = bend; fidir->fd_bbot >= finfo->fi_btop;
518 fidir->fd_bbot--, p--)
520 if (p->hf_file->f_dentry
521 && p->hf_file->f_dentry->d_inode)
526 AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
530 * after branch manipulating, refresh the file.
532 static int refresh_file(struct file *file, int (*reopen)(struct file *file))
534 int err, need_reopen;
535 aufs_bindex_t bend, bindex;
536 struct dentry *dentry;
537 struct au_finfo *finfo;
538 struct au_hfile *hfile;
540 dentry = file->f_dentry;
542 if (!finfo->fi_hdir) {
543 hfile = &finfo->fi_htop;
544 AuDebugOn(!hfile->hf_file);
545 bindex = au_br_index(dentry->d_sb, hfile->hf_br->br_id);
546 AuDebugOn(bindex < 0);
547 if (bindex != finfo->fi_btop)
548 au_set_fbstart(file, bindex);
550 err = au_fidir_realloc(finfo, au_sbend(dentry->d_sb) + 1);
553 au_do_refresh_dir(file);
558 if (!au_test_mmapped(file))
559 err = au_file_refresh_by_inode(file, &need_reopen);
560 if (!err && need_reopen && !d_unlinked(dentry))
563 au_update_figen(file);
564 goto out; /* success */
567 /* error, close all lower files */
568 if (finfo->fi_hdir) {
569 bend = au_fbend_dir(file);
570 for (bindex = au_fbstart(file); bindex <= bend; bindex++)
571 au_set_h_fptr(file, bindex, NULL);
578 /* common function to regular file and dir */
579 int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
583 unsigned int sigen, figen;
584 aufs_bindex_t bstart;
585 unsigned char pseudo_link;
586 struct dentry *dentry;
590 dentry = file->f_dentry;
591 inode = dentry->d_inode;
592 AuDebugOn(au_special_file(inode->i_mode));
593 sigen = au_sigen(dentry->d_sb);
595 figen = au_figen(file);
596 di_write_lock_child(dentry);
597 bstart = au_dbstart(dentry);
598 pseudo_link = (bstart != au_ibstart(inode));
599 if (sigen == figen && !pseudo_link && au_fbstart(file) == bstart) {
601 di_downgrade_lock(dentry, AuLock_IR);
602 fi_downgrade_lock(file);
604 goto out; /* success */
607 AuDbg("sigen %d, figen %d\n", sigen, figen);
608 if (au_digen_test(dentry, sigen)) {
609 err = au_reval_dpath(dentry, sigen);
610 AuDebugOn(!err && au_digen_test(dentry, sigen));
614 err = refresh_file(file, reopen);
617 di_downgrade_lock(dentry, AuLock_IR);
618 fi_downgrade_lock(file);
621 di_write_unlock(dentry);
622 fi_write_unlock(file);
629 /* ---------------------------------------------------------------------- */
631 /* cf. aufs_nopage() */
633 static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
639 /* it will never be called, but necessary to support O_DIRECT */
640 static ssize_t aufs_direct_IO(int rw, struct kiocb *iocb,
641 const struct iovec *iov, loff_t offset,
642 unsigned long nr_segs)
646 * it will never be called, but madvise and fadvise behaves differently
647 * when get_xip_mem is defined
649 static int aufs_get_xip_mem(struct address_space *mapping, pgoff_t pgoff,
650 int create, void **kmem, unsigned long *pfn)
653 /* they will never be called. */
654 #ifdef CONFIG_AUFS_DEBUG
655 static int aufs_write_begin(struct file *file, struct address_space *mapping,
656 loff_t pos, unsigned len, unsigned flags,
657 struct page **pagep, void **fsdata)
658 { AuUnsupport(); return 0; }
659 static int aufs_write_end(struct file *file, struct address_space *mapping,
660 loff_t pos, unsigned len, unsigned copied,
661 struct page *page, void *fsdata)
662 { AuUnsupport(); return 0; }
663 static int aufs_writepage(struct page *page, struct writeback_control *wbc)
664 { AuUnsupport(); return 0; }
666 static int aufs_set_page_dirty(struct page *page)
667 { AuUnsupport(); return 0; }
668 static void aufs_invalidatepage(struct page *page, unsigned long offset)
670 static int aufs_releasepage(struct page *page, gfp_t gfp)
671 { AuUnsupport(); return 0; }
672 static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
673 struct page *page, enum migrate_mode mode)
674 { AuUnsupport(); return 0; }
675 static int aufs_launder_page(struct page *page)
676 { AuUnsupport(); return 0; }
677 static int aufs_is_partially_uptodate(struct page *page,
678 read_descriptor_t *desc,
680 { AuUnsupport(); return 0; }
681 static int aufs_error_remove_page(struct address_space *mapping,
683 { AuUnsupport(); return 0; }
684 #endif /* CONFIG_AUFS_DEBUG */
686 const struct address_space_operations aufs_aop = {
687 .readpage = aufs_readpage,
688 .direct_IO = aufs_direct_IO,
689 .get_xip_mem = aufs_get_xip_mem,
690 #ifdef CONFIG_AUFS_DEBUG
691 .writepage = aufs_writepage,
692 /* no writepages, because of writepage */
693 .set_page_dirty = aufs_set_page_dirty,
694 /* no readpages, because of readpage */
695 .write_begin = aufs_write_begin,
696 .write_end = aufs_write_end,
697 /* no bmap, no block device */
698 .invalidatepage = aufs_invalidatepage,
699 .releasepage = aufs_releasepage,
700 .migratepage = aufs_migratepage,
701 .launder_page = aufs_launder_page,
702 .is_partially_uptodate = aufs_is_partially_uptodate,
703 .error_remove_page = aufs_error_remove_page
704 #endif /* CONFIG_AUFS_DEBUG */