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 * sub-routines for VFS
23 #include <linux/ima.h>
24 #include <linux/namei.h>
25 #include <linux/security.h>
26 #include <linux/splice.h>
29 int vfsub_update_h_iattr(struct path *h_path, int *did)
33 struct super_block *h_sb;
35 /* for remote fs, leave work for its getattr or d_revalidate */
36 /* for bad i_attr fs, handle them in aufs_getattr() */
37 /* still some fs may acquire i_mutex. we need to skip them */
41 h_sb = h_path->dentry->d_sb;
42 *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
44 err = vfs_getattr(h_path->mnt, h_path->dentry, &st);
49 /* ---------------------------------------------------------------------- */
51 struct file *vfsub_dentry_open(struct path *path, int flags)
56 file = dentry_open(path->dentry, path->mnt,
57 flags /* | __FMODE_NONOTIFY */,
59 if (!IS_ERR_OR_NULL(file)
60 && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
61 i_readcount_inc(path->dentry->d_inode);
66 struct file *vfsub_filp_open(const char *path, int oflags, int mode)
71 file = filp_open(path,
72 oflags /* | __FMODE_NONOTIFY */,
77 vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
83 int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
87 err = kern_path(name, flags, path);
88 if (!err && path->dentry->d_inode)
89 vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
93 struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
100 /* VFS checks it too, but by WARN_ON_ONCE() */
101 IMustLock(parent->d_inode);
103 path.dentry = lookup_one_len(name, parent, len);
104 if (IS_ERR(path.dentry))
106 if (path.dentry->d_inode)
107 vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
110 AuTraceErrPtr(path.dentry);
114 struct dentry *vfsub_lookup_hash(struct nameidata *nd)
120 IMustLock(nd->path.dentry->d_inode);
122 path.dentry = lookup_hash(nd);
123 if (IS_ERR(path.dentry))
125 if (path.dentry->d_inode)
126 vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
129 AuTraceErrPtr(path.dentry);
134 * this is "VFS:__lookup_one_len()" which was removed and merged into
135 * VFS:lookup_one_len() by the commit.
136 * 6a96ba5 2011-03-14 kill __lookup_one_len()
137 * this function should always be equivalent to the corresponding part in
138 * VFS:lookup_one_len().
140 int vfsub_name_hash(const char *name, struct qstr *this, int len)
150 hash = init_name_hash();
152 c = *(const unsigned char *)name++;
153 if (c == '/' || c == '\0')
155 hash = partial_name_hash(c, hash);
157 this->hash = end_name_hash(hash);
161 /* ---------------------------------------------------------------------- */
163 struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
164 struct dentry *d2, struct au_hinode *hdir2)
169 d = lock_rename(d1, d2);
171 au_hn_suspend(hdir1);
173 au_hn_suspend(hdir2);
178 void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
179 struct dentry *d2, struct au_hinode *hdir2)
185 unlock_rename(d1, d2);
189 /* ---------------------------------------------------------------------- */
191 int vfsub_create(struct inode *dir, struct path *path, int mode)
199 path->dentry = d->d_parent;
200 err = security_path_mknod(path, d, mode, 0);
205 if (au_test_fs_null_nd(dir->i_sb))
206 err = vfs_create(dir, path->dentry, mode, NULL);
208 struct nameidata h_nd;
210 memset(&h_nd, 0, sizeof(h_nd));
211 h_nd.flags = LOOKUP_CREATE;
212 h_nd.intent.open.flags = O_CREAT
213 | vfsub_fmode_to_uint(FMODE_READ);
214 h_nd.intent.open.create_mode = mode;
215 h_nd.path.dentry = path->dentry->d_parent;
216 h_nd.path.mnt = path->mnt;
217 path_get(&h_nd.path);
218 err = vfs_create(dir, path->dentry, mode, &h_nd);
219 path_put(&h_nd.path);
223 struct path tmp = *path;
226 vfsub_update_h_iattr(&tmp, &did);
228 tmp.dentry = path->dentry->d_parent;
229 vfsub_update_h_iattr(&tmp, /*did*/NULL);
238 int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
246 path->dentry = d->d_parent;
247 err = security_path_symlink(path, d, symname);
252 err = vfs_symlink(dir, path->dentry, symname);
254 struct path tmp = *path;
257 vfsub_update_h_iattr(&tmp, &did);
259 tmp.dentry = path->dentry->d_parent;
260 vfsub_update_h_iattr(&tmp, /*did*/NULL);
269 int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
277 path->dentry = d->d_parent;
278 err = security_path_mknod(path, d, mode, new_encode_dev(dev));
283 err = vfs_mknod(dir, path->dentry, mode, dev);
285 struct path tmp = *path;
288 vfsub_update_h_iattr(&tmp, &did);
290 tmp.dentry = path->dentry->d_parent;
291 vfsub_update_h_iattr(&tmp, /*did*/NULL);
300 static int au_test_nlink(struct inode *inode)
302 const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
304 if (!au_test_fs_no_limit_nlink(inode->i_sb)
305 || inode->i_nlink < link_max)
310 int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path)
317 err = au_test_nlink(src_dentry->d_inode);
322 path->dentry = d->d_parent;
323 err = security_path_link(src_dentry, path, d);
329 err = vfs_link(src_dentry, dir, path->dentry);
332 struct path tmp = *path;
335 /* fuse has different memory inode for the same inumber */
336 vfsub_update_h_iattr(&tmp, &did);
338 tmp.dentry = path->dentry->d_parent;
339 vfsub_update_h_iattr(&tmp, /*did*/NULL);
340 tmp.dentry = src_dentry;
341 vfsub_update_h_iattr(&tmp, /*did*/NULL);
350 int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
351 struct inode *dir, struct path *path)
363 path->dentry = d->d_parent;
364 tmp.dentry = src_dentry->d_parent;
365 err = security_path_rename(&tmp, src_dentry, path, d);
371 err = vfs_rename(src_dir, src_dentry, dir, path->dentry);
376 tmp.dentry = d->d_parent;
377 vfsub_update_h_iattr(&tmp, &did);
379 tmp.dentry = src_dentry;
380 vfsub_update_h_iattr(&tmp, /*did*/NULL);
381 tmp.dentry = src_dentry->d_parent;
382 vfsub_update_h_iattr(&tmp, /*did*/NULL);
391 int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
399 path->dentry = d->d_parent;
400 err = security_path_mkdir(path, d, mode);
405 err = vfs_mkdir(dir, path->dentry, mode);
407 struct path tmp = *path;
410 vfsub_update_h_iattr(&tmp, &did);
412 tmp.dentry = path->dentry->d_parent;
413 vfsub_update_h_iattr(&tmp, /*did*/NULL);
422 int vfsub_rmdir(struct inode *dir, struct path *path)
430 path->dentry = d->d_parent;
431 err = security_path_rmdir(path, d);
437 err = vfs_rmdir(dir, path->dentry);
441 .dentry = path->dentry->d_parent,
445 vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
452 /* ---------------------------------------------------------------------- */
454 /* todo: support mmap_sem? */
455 ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
461 err = vfs_read(file, ubuf, count, ppos);
464 vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
468 /* todo: kernel_read()? */
469 ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
482 err = vfsub_read_u(file, buf.u, count, ppos);
487 ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
493 err = vfs_write(file, ubuf, count, ppos);
496 vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
500 ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
506 const char __user *u;
512 err = vfsub_write_u(file, buf.u, count, ppos);
517 int vfsub_flush(struct file *file, fl_owner_t id)
522 if (file->f_op && file->f_op->flush) {
523 if (!au_test_nfs(file->f_dentry->d_sb))
524 err = file->f_op->flush(file, id);
527 err = file->f_op->flush(file, id);
531 vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
537 int vfsub_readdir(struct file *file, filldir_t filldir, void *arg)
542 err = vfs_readdir(file, filldir, arg);
545 vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
549 long vfsub_splice_to(struct file *in, loff_t *ppos,
550 struct pipe_inode_info *pipe, size_t len,
556 err = do_splice_to(in, ppos, pipe, len, flags);
560 vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
564 long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
565 loff_t *ppos, size_t len, unsigned int flags)
570 err = do_splice_from(pipe, out, ppos, len, flags);
573 vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
577 int vfsub_fsync(struct file *file, struct path *path, int datasync)
581 /* file can be NULL */
583 err = vfs_fsync(file, datasync);
588 path = &file->f_path;
590 vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
595 /* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
596 int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
600 struct inode *h_inode;
602 h_inode = h_path->dentry->d_inode;
604 err = mnt_want_write(h_path->mnt);
607 err = inode_permission(h_inode, MAY_WRITE);
610 err = get_write_access(h_inode);
613 err = break_lease(h_inode, O_WRONLY);
618 err = locks_verify_truncate(h_inode, h_file, length);
620 err = security_path_truncate(h_path);
623 err = do_truncate(h_path->dentry, length, attr, h_file);
629 put_write_access(h_inode);
632 mnt_drop_write(h_path->mnt);
637 /* ---------------------------------------------------------------------- */
639 struct au_vfsub_mkdir_args {
646 static void au_call_vfsub_mkdir(void *args)
648 struct au_vfsub_mkdir_args *a = args;
649 *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
652 int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
654 int err, do_sio, wkq_err;
656 do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
658 err = vfsub_mkdir(dir, path, mode);
660 struct au_vfsub_mkdir_args args = {
666 wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
667 if (unlikely(wkq_err))
674 struct au_vfsub_rmdir_args {
680 static void au_call_vfsub_rmdir(void *args)
682 struct au_vfsub_rmdir_args *a = args;
683 *a->errp = vfsub_rmdir(a->dir, a->path);
686 int vfsub_sio_rmdir(struct inode *dir, struct path *path)
688 int err, do_sio, wkq_err;
690 do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
692 err = vfsub_rmdir(dir, path);
694 struct au_vfsub_rmdir_args args = {
699 wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
700 if (unlikely(wkq_err))
707 /* ---------------------------------------------------------------------- */
709 struct notify_change_args {
715 static void call_notify_change(void *args)
717 struct notify_change_args *a = args;
718 struct inode *h_inode;
720 h_inode = a->path->dentry->d_inode;
724 if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
725 *a->errp = notify_change(a->path->dentry, a->ia);
727 vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
729 AuTraceErr(*a->errp);
732 int vfsub_notify_change(struct path *path, struct iattr *ia)
735 struct notify_change_args args = {
741 call_notify_change(&args);
746 int vfsub_sio_notify_change(struct path *path, struct iattr *ia)
749 struct notify_change_args args = {
755 wkq_err = au_wkq_wait(call_notify_change, &args);
756 if (unlikely(wkq_err))
762 /* ---------------------------------------------------------------------- */
770 static void call_unlink(void *args)
772 struct unlink_args *a = args;
773 struct dentry *d = a->path->dentry;
774 struct inode *h_inode;
775 const int stop_sillyrename = (au_test_nfs(d->d_sb)
780 a->path->dentry = d->d_parent;
781 *a->errp = security_path_unlink(a->path, d);
783 if (unlikely(*a->errp))
786 if (!stop_sillyrename)
788 h_inode = d->d_inode;
793 *a->errp = vfs_unlink(a->dir, d);
797 .dentry = d->d_parent,
800 vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
803 if (!stop_sillyrename)
808 AuTraceErr(*a->errp);
812 * @dir: must be locked.
813 * @dentry: target dentry.
815 int vfsub_unlink(struct inode *dir, struct path *path, int force)
818 struct unlink_args args = {
829 wkq_err = au_wkq_wait(call_unlink, &args);
830 if (unlikely(wkq_err))