331ab17fb5c175725ee3beefc1def98d078107f9
[pandora-kernel.git] / fs / aufs / vfsub.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  * sub-routines for VFS
21  */
22
23 #include <linux/ima.h>
24 #include <linux/namei.h>
25 #include <linux/security.h>
26 #include <linux/splice.h>
27 #include "aufs.h"
28
29 int vfsub_update_h_iattr(struct path *h_path, int *did)
30 {
31         int err;
32         struct kstat st;
33         struct super_block *h_sb;
34
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 */
38         err = 0;
39         if (!did)
40                 did = &err;
41         h_sb = h_path->dentry->d_sb;
42         *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
43         if (*did)
44                 err = vfs_getattr(h_path->mnt, h_path->dentry, &st);
45
46         return err;
47 }
48
49 /* ---------------------------------------------------------------------- */
50
51 struct file *vfsub_dentry_open(struct path *path, int flags)
52 {
53         struct file *file;
54
55         path_get(path);
56         file = dentry_open(path->dentry, path->mnt,
57                            flags /* | __FMODE_NONOTIFY */,
58                            current_cred());
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);
62
63         return file;
64 }
65
66 struct file *vfsub_filp_open(const char *path, int oflags, int mode)
67 {
68         struct file *file;
69
70         lockdep_off();
71         file = filp_open(path,
72                          oflags /* | __FMODE_NONOTIFY */,
73                          mode);
74         lockdep_on();
75         if (IS_ERR(file))
76                 goto out;
77         vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
78
79 out:
80         return file;
81 }
82
83 int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
84 {
85         int err;
86
87         err = kern_path(name, flags, path);
88         if (!err && path->dentry->d_inode)
89                 vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
90         return err;
91 }
92
93 struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
94                                     int len)
95 {
96         struct path path = {
97                 .mnt = NULL
98         };
99
100         /* VFS checks it too, but by WARN_ON_ONCE() */
101         IMustLock(parent->d_inode);
102
103         path.dentry = lookup_one_len(name, parent, len);
104         if (IS_ERR(path.dentry))
105                 goto out;
106         if (path.dentry->d_inode)
107                 vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
108
109 out:
110         AuTraceErrPtr(path.dentry);
111         return path.dentry;
112 }
113
114 struct dentry *vfsub_lookup_hash(struct nameidata *nd)
115 {
116         struct path path = {
117                 .mnt = nd->path.mnt
118         };
119
120         IMustLock(nd->path.dentry->d_inode);
121
122         path.dentry = lookup_hash(nd);
123         if (IS_ERR(path.dentry))
124                 goto out;
125         if (path.dentry->d_inode)
126                 vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
127
128 out:
129         AuTraceErrPtr(path.dentry);
130         return path.dentry;
131 }
132
133 /*
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().
139  */
140 int vfsub_name_hash(const char *name, struct qstr *this, int len)
141 {
142         unsigned long hash;
143         unsigned int c;
144
145         this->name = name;
146         this->len = len;
147         if (!len)
148                 return -EACCES;
149
150         hash = init_name_hash();
151         while (len--) {
152                 c = *(const unsigned char *)name++;
153                 if (c == '/' || c == '\0')
154                         return -EACCES;
155                 hash = partial_name_hash(c, hash);
156         }
157         this->hash = end_name_hash(hash);
158         return 0;
159 }
160
161 /* ---------------------------------------------------------------------- */
162
163 struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
164                                  struct dentry *d2, struct au_hinode *hdir2)
165 {
166         struct dentry *d;
167
168         lockdep_off();
169         d = lock_rename(d1, d2);
170         lockdep_on();
171         au_hn_suspend(hdir1);
172         if (hdir1 != hdir2)
173                 au_hn_suspend(hdir2);
174
175         return d;
176 }
177
178 void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
179                          struct dentry *d2, struct au_hinode *hdir2)
180 {
181         au_hn_resume(hdir1);
182         if (hdir1 != hdir2)
183                 au_hn_resume(hdir2);
184         lockdep_off();
185         unlock_rename(d1, d2);
186         lockdep_on();
187 }
188
189 /* ---------------------------------------------------------------------- */
190
191 int vfsub_create(struct inode *dir, struct path *path, int mode)
192 {
193         int err;
194         struct dentry *d;
195
196         IMustLock(dir);
197
198         d = path->dentry;
199         path->dentry = d->d_parent;
200         err = security_path_mknod(path, d, mode, 0);
201         path->dentry = d;
202         if (unlikely(err))
203                 goto out;
204
205         if (au_test_fs_null_nd(dir->i_sb))
206                 err = vfs_create(dir, path->dentry, mode, NULL);
207         else {
208                 struct nameidata h_nd;
209
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);
220         }
221
222         if (!err) {
223                 struct path tmp = *path;
224                 int did;
225
226                 vfsub_update_h_iattr(&tmp, &did);
227                 if (did) {
228                         tmp.dentry = path->dentry->d_parent;
229                         vfsub_update_h_iattr(&tmp, /*did*/NULL);
230                 }
231                 /*ignore*/
232         }
233
234 out:
235         return err;
236 }
237
238 int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
239 {
240         int err;
241         struct dentry *d;
242
243         IMustLock(dir);
244
245         d = path->dentry;
246         path->dentry = d->d_parent;
247         err = security_path_symlink(path, d, symname);
248         path->dentry = d;
249         if (unlikely(err))
250                 goto out;
251
252         err = vfs_symlink(dir, path->dentry, symname);
253         if (!err) {
254                 struct path tmp = *path;
255                 int did;
256
257                 vfsub_update_h_iattr(&tmp, &did);
258                 if (did) {
259                         tmp.dentry = path->dentry->d_parent;
260                         vfsub_update_h_iattr(&tmp, /*did*/NULL);
261                 }
262                 /*ignore*/
263         }
264
265 out:
266         return err;
267 }
268
269 int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
270 {
271         int err;
272         struct dentry *d;
273
274         IMustLock(dir);
275
276         d = path->dentry;
277         path->dentry = d->d_parent;
278         err = security_path_mknod(path, d, mode, new_encode_dev(dev));
279         path->dentry = d;
280         if (unlikely(err))
281                 goto out;
282
283         err = vfs_mknod(dir, path->dentry, mode, dev);
284         if (!err) {
285                 struct path tmp = *path;
286                 int did;
287
288                 vfsub_update_h_iattr(&tmp, &did);
289                 if (did) {
290                         tmp.dentry = path->dentry->d_parent;
291                         vfsub_update_h_iattr(&tmp, /*did*/NULL);
292                 }
293                 /*ignore*/
294         }
295
296 out:
297         return err;
298 }
299
300 static int au_test_nlink(struct inode *inode)
301 {
302         const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
303
304         if (!au_test_fs_no_limit_nlink(inode->i_sb)
305             || inode->i_nlink < link_max)
306                 return 0;
307         return -EMLINK;
308 }
309
310 int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path)
311 {
312         int err;
313         struct dentry *d;
314
315         IMustLock(dir);
316
317         err = au_test_nlink(src_dentry->d_inode);
318         if (unlikely(err))
319                 return err;
320
321         d = path->dentry;
322         path->dentry = d->d_parent;
323         err = security_path_link(src_dentry, path, d);
324         path->dentry = d;
325         if (unlikely(err))
326                 goto out;
327
328         lockdep_off();
329         err = vfs_link(src_dentry, dir, path->dentry);
330         lockdep_on();
331         if (!err) {
332                 struct path tmp = *path;
333                 int did;
334
335                 /* fuse has different memory inode for the same inumber */
336                 vfsub_update_h_iattr(&tmp, &did);
337                 if (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);
342                 }
343                 /*ignore*/
344         }
345
346 out:
347         return err;
348 }
349
350 int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
351                  struct inode *dir, struct path *path)
352 {
353         int err;
354         struct path tmp = {
355                 .mnt    = path->mnt
356         };
357         struct dentry *d;
358
359         IMustLock(dir);
360         IMustLock(src_dir);
361
362         d = path->dentry;
363         path->dentry = d->d_parent;
364         tmp.dentry = src_dentry->d_parent;
365         err = security_path_rename(&tmp, src_dentry, path, d);
366         path->dentry = d;
367         if (unlikely(err))
368                 goto out;
369
370         lockdep_off();
371         err = vfs_rename(src_dir, src_dentry, dir, path->dentry);
372         lockdep_on();
373         if (!err) {
374                 int did;
375
376                 tmp.dentry = d->d_parent;
377                 vfsub_update_h_iattr(&tmp, &did);
378                 if (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);
383                 }
384                 /*ignore*/
385         }
386
387 out:
388         return err;
389 }
390
391 int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
392 {
393         int err;
394         struct dentry *d;
395
396         IMustLock(dir);
397
398         d = path->dentry;
399         path->dentry = d->d_parent;
400         err = security_path_mkdir(path, d, mode);
401         path->dentry = d;
402         if (unlikely(err))
403                 goto out;
404
405         err = vfs_mkdir(dir, path->dentry, mode);
406         if (!err) {
407                 struct path tmp = *path;
408                 int did;
409
410                 vfsub_update_h_iattr(&tmp, &did);
411                 if (did) {
412                         tmp.dentry = path->dentry->d_parent;
413                         vfsub_update_h_iattr(&tmp, /*did*/NULL);
414                 }
415                 /*ignore*/
416         }
417
418 out:
419         return err;
420 }
421
422 int vfsub_rmdir(struct inode *dir, struct path *path)
423 {
424         int err;
425         struct dentry *d;
426
427         IMustLock(dir);
428
429         d = path->dentry;
430         path->dentry = d->d_parent;
431         err = security_path_rmdir(path, d);
432         path->dentry = d;
433         if (unlikely(err))
434                 goto out;
435
436         lockdep_off();
437         err = vfs_rmdir(dir, path->dentry);
438         lockdep_on();
439         if (!err) {
440                 struct path tmp = {
441                         .dentry = path->dentry->d_parent,
442                         .mnt    = path->mnt
443                 };
444
445                 vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
446         }
447
448 out:
449         return err;
450 }
451
452 /* ---------------------------------------------------------------------- */
453
454 /* todo: support mmap_sem? */
455 ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
456                      loff_t *ppos)
457 {
458         ssize_t err;
459
460         lockdep_off();
461         err = vfs_read(file, ubuf, count, ppos);
462         lockdep_on();
463         if (err >= 0)
464                 vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
465         return err;
466 }
467
468 /* todo: kernel_read()? */
469 ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
470                      loff_t *ppos)
471 {
472         ssize_t err;
473         mm_segment_t oldfs;
474         union {
475                 void *k;
476                 char __user *u;
477         } buf;
478
479         buf.k = kbuf;
480         oldfs = get_fs();
481         set_fs(KERNEL_DS);
482         err = vfsub_read_u(file, buf.u, count, ppos);
483         set_fs(oldfs);
484         return err;
485 }
486
487 ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
488                       loff_t *ppos)
489 {
490         ssize_t err;
491
492         lockdep_off();
493         err = vfs_write(file, ubuf, count, ppos);
494         lockdep_on();
495         if (err >= 0)
496                 vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
497         return err;
498 }
499
500 ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
501 {
502         ssize_t err;
503         mm_segment_t oldfs;
504         union {
505                 void *k;
506                 const char __user *u;
507         } buf;
508
509         buf.k = kbuf;
510         oldfs = get_fs();
511         set_fs(KERNEL_DS);
512         err = vfsub_write_u(file, buf.u, count, ppos);
513         set_fs(oldfs);
514         return err;
515 }
516
517 int vfsub_flush(struct file *file, fl_owner_t id)
518 {
519         int err;
520
521         err = 0;
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);
525                 else {
526                         lockdep_off();
527                         err = file->f_op->flush(file, id);
528                         lockdep_on();
529                 }
530                 if (!err)
531                         vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
532                 /*ignore*/
533         }
534         return err;
535 }
536
537 int vfsub_readdir(struct file *file, filldir_t filldir, void *arg)
538 {
539         int err;
540
541         lockdep_off();
542         err = vfs_readdir(file, filldir, arg);
543         lockdep_on();
544         if (err >= 0)
545                 vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
546         return err;
547 }
548
549 long vfsub_splice_to(struct file *in, loff_t *ppos,
550                      struct pipe_inode_info *pipe, size_t len,
551                      unsigned int flags)
552 {
553         long err;
554
555         lockdep_off();
556         err = do_splice_to(in, ppos, pipe, len, flags);
557         lockdep_on();
558         file_accessed(in);
559         if (err >= 0)
560                 vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
561         return err;
562 }
563
564 long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
565                        loff_t *ppos, size_t len, unsigned int flags)
566 {
567         long err;
568
569         lockdep_off();
570         err = do_splice_from(pipe, out, ppos, len, flags);
571         lockdep_on();
572         if (err >= 0)
573                 vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
574         return err;
575 }
576
577 int vfsub_fsync(struct file *file, struct path *path, int datasync)
578 {
579         int err;
580
581         /* file can be NULL */
582         lockdep_off();
583         err = vfs_fsync(file, datasync);
584         lockdep_on();
585         if (!err) {
586                 if (!path) {
587                         AuDebugOn(!file);
588                         path = &file->f_path;
589                 }
590                 vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
591         }
592         return err;
593 }
594
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,
597                 struct file *h_file)
598 {
599         int err;
600         struct inode *h_inode;
601
602         h_inode = h_path->dentry->d_inode;
603         if (!h_file) {
604                 err = mnt_want_write(h_path->mnt);
605                 if (err)
606                         goto out;
607                 err = inode_permission(h_inode, MAY_WRITE);
608                 if (err)
609                         goto out_mnt;
610                 err = get_write_access(h_inode);
611                 if (err)
612                         goto out_mnt;
613                 err = break_lease(h_inode, O_WRONLY);
614                 if (err)
615                         goto out_inode;
616         }
617
618         err = locks_verify_truncate(h_inode, h_file, length);
619         if (!err)
620                 err = security_path_truncate(h_path);
621         if (!err) {
622                 lockdep_off();
623                 err = do_truncate(h_path->dentry, length, attr, h_file);
624                 lockdep_on();
625         }
626
627 out_inode:
628         if (!h_file)
629                 put_write_access(h_inode);
630 out_mnt:
631         if (!h_file)
632                 mnt_drop_write(h_path->mnt);
633 out:
634         return err;
635 }
636
637 /* ---------------------------------------------------------------------- */
638
639 struct au_vfsub_mkdir_args {
640         int *errp;
641         struct inode *dir;
642         struct path *path;
643         int mode;
644 };
645
646 static void au_call_vfsub_mkdir(void *args)
647 {
648         struct au_vfsub_mkdir_args *a = args;
649         *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
650 }
651
652 int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
653 {
654         int err, do_sio, wkq_err;
655
656         do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
657         if (!do_sio)
658                 err = vfsub_mkdir(dir, path, mode);
659         else {
660                 struct au_vfsub_mkdir_args args = {
661                         .errp   = &err,
662                         .dir    = dir,
663                         .path   = path,
664                         .mode   = mode
665                 };
666                 wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
667                 if (unlikely(wkq_err))
668                         err = wkq_err;
669         }
670
671         return err;
672 }
673
674 struct au_vfsub_rmdir_args {
675         int *errp;
676         struct inode *dir;
677         struct path *path;
678 };
679
680 static void au_call_vfsub_rmdir(void *args)
681 {
682         struct au_vfsub_rmdir_args *a = args;
683         *a->errp = vfsub_rmdir(a->dir, a->path);
684 }
685
686 int vfsub_sio_rmdir(struct inode *dir, struct path *path)
687 {
688         int err, do_sio, wkq_err;
689
690         do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
691         if (!do_sio)
692                 err = vfsub_rmdir(dir, path);
693         else {
694                 struct au_vfsub_rmdir_args args = {
695                         .errp   = &err,
696                         .dir    = dir,
697                         .path   = path
698                 };
699                 wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
700                 if (unlikely(wkq_err))
701                         err = wkq_err;
702         }
703
704         return err;
705 }
706
707 /* ---------------------------------------------------------------------- */
708
709 struct notify_change_args {
710         int *errp;
711         struct path *path;
712         struct iattr *ia;
713 };
714
715 static void call_notify_change(void *args)
716 {
717         struct notify_change_args *a = args;
718         struct inode *h_inode;
719
720         h_inode = a->path->dentry->d_inode;
721         IMustLock(h_inode);
722
723         *a->errp = -EPERM;
724         if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
725                 *a->errp = notify_change(a->path->dentry, a->ia);
726                 if (!*a->errp)
727                         vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
728         }
729         AuTraceErr(*a->errp);
730 }
731
732 int vfsub_notify_change(struct path *path, struct iattr *ia)
733 {
734         int err;
735         struct notify_change_args args = {
736                 .errp   = &err,
737                 .path   = path,
738                 .ia     = ia
739         };
740
741         call_notify_change(&args);
742
743         return err;
744 }
745
746 int vfsub_sio_notify_change(struct path *path, struct iattr *ia)
747 {
748         int err, wkq_err;
749         struct notify_change_args args = {
750                 .errp   = &err,
751                 .path   = path,
752                 .ia     = ia
753         };
754
755         wkq_err = au_wkq_wait(call_notify_change, &args);
756         if (unlikely(wkq_err))
757                 err = wkq_err;
758
759         return err;
760 }
761
762 /* ---------------------------------------------------------------------- */
763
764 struct unlink_args {
765         int *errp;
766         struct inode *dir;
767         struct path *path;
768 };
769
770 static void call_unlink(void *args)
771 {
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)
776                                       && d->d_count == 1);
777
778         IMustLock(a->dir);
779
780         a->path->dentry = d->d_parent;
781         *a->errp = security_path_unlink(a->path, d);
782         a->path->dentry = d;
783         if (unlikely(*a->errp))
784                 return;
785
786         if (!stop_sillyrename)
787                 dget(d);
788         h_inode = d->d_inode;
789         if (h_inode)
790                 ihold(h_inode);
791
792         lockdep_off();
793         *a->errp = vfs_unlink(a->dir, d);
794         lockdep_on();
795         if (!*a->errp) {
796                 struct path tmp = {
797                         .dentry = d->d_parent,
798                         .mnt    = a->path->mnt
799                 };
800                 vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
801         }
802
803         if (!stop_sillyrename)
804                 dput(d);
805         if (h_inode)
806                 iput(h_inode);
807
808         AuTraceErr(*a->errp);
809 }
810
811 /*
812  * @dir: must be locked.
813  * @dentry: target dentry.
814  */
815 int vfsub_unlink(struct inode *dir, struct path *path, int force)
816 {
817         int err;
818         struct unlink_args args = {
819                 .errp   = &err,
820                 .dir    = dir,
821                 .path   = path
822         };
823
824         if (!force)
825                 call_unlink(&args);
826         else {
827                 int wkq_err;
828
829                 wkq_err = au_wkq_wait(call_unlink, &args);
830                 if (unlikely(wkq_err))
831                         err = wkq_err;
832         }
833
834         return err;
835 }