2 * Copyright (C) 2005-2012 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 #include <linux/pagemap.h>
26 /* drop flags for writing */
27 unsigned int au_file_roflags(unsigned int flags)
29 flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
30 flags |= O_RDONLY | O_NOATIME;
34 /* common functions to regular file and dir */
35 struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
39 struct dentry *h_dentry;
40 struct inode *h_inode;
41 struct super_block *sb;
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)
51 h_inode = h_dentry->d_inode;
52 if (au_test_nfsd() && !h_inode)
54 spin_lock(&h_dentry->d_lock);
55 err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
57 /* || !dentry->d_inode->i_nlink */
59 spin_unlock(&h_dentry->d_lock);
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))
70 /* drop flags for writing */
71 if (au_test_ro(sb, bindex, dentry->d_inode))
72 flags = au_file_roflags(flags);
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);
80 /* this block depends upon the configuration */
81 di_read_unlock(dentry, AuLock_IR);
82 fi_write_unlock(file);
84 h_file = vfsub_dentry_open(&h_path, flags);
85 si_noflush_read_lock(sb);
87 di_read_lock_child(dentry, AuLock_IR);
93 err = deny_write_access(h_file);
96 h_file = ERR_PTR(err);
100 fsnotify_open(h_file);
101 goto out; /* success */
104 atomic_dec(&br->br_count);
109 int au_do_open(struct file *file, int (*open)(struct file *file, int flags),
110 struct au_fidir *fidir)
113 struct dentry *dentry;
115 err = au_finfo_init(file, fidir);
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);
124 fi_write_unlock(file);
126 au_fi(file)->fi_hdir = NULL;
134 int au_reopen_nondir(struct file *file)
137 aufs_bindex_t bstart;
138 struct dentry *dentry;
139 struct file *h_file, *h_file_tmp;
141 dentry = file->f_dentry;
142 AuDebugOn(au_special_file(dentry->d_inode->i_mode));
143 bstart = au_dbstart(dentry);
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 */
150 get_file(h_file_tmp);
151 au_set_h_fptr(file, bstart, NULL);
153 AuDebugOn(au_fi(file)->fi_hdir);
154 AuDebugOn(au_fbstart(file) < bstart);
156 h_file = au_h_open(dentry, bstart, vfsub_file_flags(file) & ~O_TRUNC,
158 err = PTR_ERR(h_file);
160 goto out; /* todo: close all? */
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; */
175 /* ---------------------------------------------------------------------- */
177 static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
178 struct dentry *hi_wh)
181 aufs_bindex_t bstart;
182 struct au_dinfo *dinfo;
183 struct dentry *h_dentry;
184 struct au_hdentry *hdp;
186 dinfo = au_di(file->f_dentry);
187 AuRwMustWriteLock(&dinfo->di_rwsem);
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;
201 static int au_ready_to_write_wh(struct file *file, loff_t len,
205 struct inode *inode, *h_inode;
206 struct dentry *dentry, *h_dentry, *hi_wh;
208 dentry = file->f_dentry;
209 au_update_dbstart(dentry);
210 inode = dentry->d_inode;
212 if (au_dbstart(dentry) <= bcpup && au_dbend(dentry) >= bcpup) {
213 h_dentry = au_h_dptr(dentry, bcpup);
215 h_inode = h_dentry->d_inode;
217 hi_wh = au_hi_wh(inode, bcpup);
218 if (!hi_wh && !h_inode)
219 err = au_sio_cpup_wh(dentry, bcpup, len, file);
221 /* already copied-up after unlink */
222 err = au_reopen_wh(file, bcpup, hi_wh);
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));
233 * prepare the @file for writing.
235 int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
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;
244 dentry = file->f_dentry;
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);
255 /* need to cpup or reopen */
256 parent = dget_parent(dentry);
257 di_write_lock_parent(parent);
258 err = AuWbrCopyup(au_sbi(sb), dentry);
260 if (unlikely(err < 0))
264 if (!d_unhashed(dentry) && !au_h_dptr(parent, bcpup)) {
265 err = au_cpup_dirs(dentry, bcpup);
270 err = au_pin(pin, dentry, bcpup, AuOpt_UDBA_NONE,
271 AuPin_DI_LOCKED | AuPin_MNT_WRITE);
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;
286 if (dbstart <= bcpup /* just reopen */
287 || !d_unhashed(dentry) /* copyup and reopen */
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);
295 di_downgrade_lock(parent, AuLock_IR);
297 err = au_sio_cpup_simple(dentry, bcpup, len,
300 err = au_reopen_nondir(file);
302 mutex_unlock(&h_inode->i_mutex);
303 au_h_open_post(dentry, bstart, h_file);
304 } else { /* copyup as wh and reopen */
306 * since writable hfsplus branch is not supported,
307 * h_open_pre/post() are unnecessary.
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);
316 au_pin_set_parent_lflag(pin, /*lflag*/0);
317 goto out_dput; /* success */
323 di_downgrade_lock(parent, AuLock_IR);
325 di_read_unlock(parent, AuLock_IR);
332 /* ---------------------------------------------------------------------- */
334 int au_do_flush(struct file *file, fl_owner_t id,
335 int (*flush)(struct file *file, fl_owner_t id))
338 struct dentry *dentry;
339 struct super_block *sb;
342 dentry = file->f_dentry;
344 inode = dentry->d_inode;
345 si_noflush_read_lock(sb);
347 ii_read_lock_child(inode);
349 err = flush(file, id);
350 au_cpup_attr_timesizes(inode);
352 ii_read_unlock(inode);
353 fi_read_unlock(file);
358 /* ---------------------------------------------------------------------- */
360 static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
363 aufs_bindex_t bstart;
365 struct au_finfo *finfo;
366 struct dentry *dentry, *parent, *hi_wh;
368 struct super_block *sb;
370 FiMustWriteLock(file);
374 dentry = file->f_dentry;
376 inode = dentry->d_inode;
377 bstart = au_ibstart(inode);
378 if (bstart == finfo->fi_btop || IS_ROOT(dentry))
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);
386 di_read_unlock(parent, !AuLock_IR);
387 if (unlikely(err < 0))
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);
402 /* always superio. */
403 err = au_pin(&pin, dentry, bstart, AuOpt_UDBA_NONE,
404 AuPin_DI_LOCKED | AuPin_MNT_WRITE);
406 err = au_sio_cpup_simple(dentry, bstart, -1,
410 /* already copied-up after unlink */
411 err = au_reopen_wh(file, bstart, hi_wh);
416 di_read_unlock(parent, AuLock_IR);
423 static void au_do_refresh_dir(struct file *file)
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;
431 FiMustWriteLock(file);
433 sb = file->f_dentry->d_sb;
435 fidir = finfo->fi_hdir;
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++) {
444 new_bindex = au_br_index(sb, p->hf_br->br_id);
445 if (new_bindex == bindex)
447 if (new_bindex < 0) {
448 au_set_h_fptr(file, bindex, NULL);
452 /* swap two lower inode, and loop again */
453 q = fidir->fd_hfile + new_bindex;
464 if (!au_test_mmapped(file) && !d_unlinked(file->f_dentry)) {
466 for (finfo->fi_btop = 0; finfo->fi_btop <= bend;
467 finfo->fi_btop++, p++)
469 if (p->hf_file->f_dentry
470 && p->hf_file->f_dentry->d_inode)
476 bend = au_br_index(sb, brid);
477 for (finfo->fi_btop = 0; finfo->fi_btop < bend;
478 finfo->fi_btop++, p++)
484 p = fidir->fd_hfile + bend;
485 for (fidir->fd_bbot = bend; fidir->fd_bbot >= finfo->fi_btop;
486 fidir->fd_bbot--, p--)
488 if (p->hf_file->f_dentry
489 && p->hf_file->f_dentry->d_inode)
494 AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
498 * after branch manipulating, refresh the file.
500 static int refresh_file(struct file *file, int (*reopen)(struct file *file))
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;
508 dentry = file->f_dentry;
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);
518 err = au_fidir_realloc(finfo, au_sbend(dentry->d_sb) + 1);
521 au_do_refresh_dir(file);
526 if (!au_test_mmapped(file))
527 err = au_file_refresh_by_inode(file, &need_reopen);
528 if (!err && need_reopen && !d_unlinked(dentry))
531 au_update_figen(file);
532 goto out; /* success */
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);
546 /* common function to regular file and dir */
547 int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
551 unsigned int sigen, figen;
552 aufs_bindex_t bstart;
553 unsigned char pseudo_link;
554 struct dentry *dentry;
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);
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) {
569 di_downgrade_lock(dentry, AuLock_IR);
570 fi_downgrade_lock(file);
572 goto out; /* success */
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));
582 err = refresh_file(file, reopen);
585 di_downgrade_lock(dentry, AuLock_IR);
586 fi_downgrade_lock(file);
589 di_write_unlock(dentry);
590 fi_write_unlock(file);
597 /* ---------------------------------------------------------------------- */
599 /* cf. aufs_nopage() */
601 static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
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)
614 * it will never be called, but madvise and fadvise behaves differently
615 * when get_xip_mem is defined
617 static int aufs_get_xip_mem(struct address_space *mapping, pgoff_t pgoff,
618 int create, void **kmem, unsigned long *pfn)
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; }
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)
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,
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,
648 { AuUnsupport(); return 0; }
649 static int aufs_error_remove_page(struct address_space *mapping,
651 { AuUnsupport(); return 0; }
652 #endif /* CONFIG_AUFS_DEBUG */
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 */