Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / fs / aufs / i_op.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  * inode operations (except add/del/rename)
21  */
22
23 #include <linux/device_cgroup.h>
24 #include <linux/fs_stack.h>
25 #include <linux/namei.h>
26 #include <linux/security.h>
27 #include "aufs.h"
28
29 static int h_permission(struct inode *h_inode, int mask,
30                         struct vfsmount *h_mnt, int brperm)
31 {
32         int err;
33         const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
34
35         err = -EACCES;
36         if ((write_mask && IS_IMMUTABLE(h_inode))
37             || ((mask & MAY_EXEC)
38                 && S_ISREG(h_inode->i_mode)
39                 && ((h_mnt->mnt_flags & MNT_NOEXEC)
40                     || !(h_inode->i_mode & S_IXUGO))))
41                 goto out;
42
43         /*
44          * - skip the lower fs test in the case of write to ro branch.
45          * - nfs dir permission write check is optimized, but a policy for
46          *   link/rename requires a real check.
47          */
48         if ((write_mask && !au_br_writable(brperm))
49             || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
50                 && write_mask && !(mask & MAY_READ))
51             || !h_inode->i_op->permission) {
52                 /* AuLabel(generic_permission); */
53                 err = generic_permission(h_inode, mask);
54         } else {
55                 /* AuLabel(h_inode->permission); */
56                 err = h_inode->i_op->permission(h_inode, mask);
57                 AuTraceErr(err);
58         }
59
60         if (!err)
61                 err = devcgroup_inode_permission(h_inode, mask);
62         if (!err)
63                 err = security_inode_permission(h_inode, mask);
64
65 #if 0
66         if (!err) {
67                 /* todo: do we need to call ima_path_check()? */
68                 struct path h_path = {
69                         .dentry =
70                         .mnt    = h_mnt
71                 };
72                 err = ima_path_check(&h_path,
73                                      mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
74                                      IMA_COUNT_LEAVE);
75         }
76 #endif
77
78 out:
79         return err;
80 }
81
82 static int aufs_permission(struct inode *inode, int mask)
83 {
84         int err;
85         aufs_bindex_t bindex, bend;
86         const unsigned char isdir = !!S_ISDIR(inode->i_mode),
87                 write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
88         struct inode *h_inode;
89         struct super_block *sb;
90         struct au_branch *br;
91
92         /* todo: support rcu-walk? */
93         if (mask & MAY_NOT_BLOCK)
94                 return -ECHILD;
95
96         sb = inode->i_sb;
97         si_read_lock(sb, AuLock_FLUSH);
98         ii_read_lock_child(inode);
99 #if 0
100         err = au_iigen_test(inode, au_sigen(sb));
101         if (unlikely(err))
102                 goto out;
103 #endif
104
105         if (!isdir || write_mask) {
106                 err = au_busy_or_stale();
107                 h_inode = au_h_iptr(inode, au_ibstart(inode));
108                 if (unlikely(!h_inode
109                              || (h_inode->i_mode & S_IFMT)
110                              != (inode->i_mode & S_IFMT)))
111                         goto out;
112
113                 err = 0;
114                 bindex = au_ibstart(inode);
115                 br = au_sbr(sb, bindex);
116                 err = h_permission(h_inode, mask, br->br_mnt, br->br_perm);
117                 if (write_mask
118                     && !err
119                     && !special_file(h_inode->i_mode)) {
120                         /* test whether the upper writable branch exists */
121                         err = -EROFS;
122                         for (; bindex >= 0; bindex--)
123                                 if (!au_br_rdonly(au_sbr(sb, bindex))) {
124                                         err = 0;
125                                         break;
126                                 }
127                 }
128                 goto out;
129         }
130
131         /* non-write to dir */
132         err = 0;
133         bend = au_ibend(inode);
134         for (bindex = au_ibstart(inode); !err && bindex <= bend; bindex++) {
135                 h_inode = au_h_iptr(inode, bindex);
136                 if (h_inode) {
137                         err = au_busy_or_stale();
138                         if (unlikely(!S_ISDIR(h_inode->i_mode)))
139                                 break;
140
141                         br = au_sbr(sb, bindex);
142                         err = h_permission(h_inode, mask, br->br_mnt,
143                                            br->br_perm);
144                 }
145         }
146
147 out:
148         ii_read_unlock(inode);
149         si_read_unlock(sb);
150         return err;
151 }
152
153 /* ---------------------------------------------------------------------- */
154
155 static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
156                                   struct nameidata *nd)
157 {
158         struct dentry *ret, *parent;
159         struct inode *inode;
160         struct super_block *sb;
161         int err, npositive, lc_idx;
162
163         IMustLock(dir);
164
165         sb = dir->i_sb;
166         err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
167         ret = ERR_PTR(err);
168         if (unlikely(err))
169                 goto out;
170
171         ret = ERR_PTR(-ENAMETOOLONG);
172         if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
173                 goto out_si;
174         err = au_di_init(dentry);
175         ret = ERR_PTR(err);
176         if (unlikely(err))
177                 goto out_si;
178
179         inode = NULL;
180         npositive = 0; /* suppress a warning */
181         parent = dentry->d_parent; /* dir inode is locked */
182         di_read_lock_parent(parent, AuLock_IR);
183         err = au_alive_dir(parent);
184         if (!err)
185                 err = au_digen_test(parent, au_sigen(sb));
186         if (!err) {
187                 npositive = au_lkup_dentry(dentry, au_dbstart(parent),
188                                            /*type*/0, nd);
189                 err = npositive;
190         }
191         di_read_unlock(parent, AuLock_IR);
192         ret = ERR_PTR(err);
193         if (unlikely(err < 0))
194                 goto out_unlock;
195
196         if (npositive) {
197                 inode = au_new_inode(dentry, /*must_new*/0);
198                 ret = (void *)inode;
199         }
200         if (IS_ERR(inode)) {
201                 inode = NULL;
202                 goto out_unlock;
203         }
204
205         ret = d_splice_alias(inode, dentry);
206         if (unlikely(IS_ERR(ret) && inode)) {
207                 ii_write_unlock(inode);
208                 lc_idx = AuLcNonDir_IIINFO;
209                 if (S_ISLNK(inode->i_mode))
210                         lc_idx = AuLcSymlink_IIINFO;
211                 else if (S_ISDIR(inode->i_mode))
212                         lc_idx = AuLcDir_IIINFO;
213                 au_rw_class(&au_ii(inode)->ii_rwsem, au_lc_key + lc_idx);
214                 iput(inode);
215         }
216
217 out_unlock:
218         di_write_unlock(dentry);
219         if (unlikely(IS_ERR(ret) && inode)) {
220                 lc_idx = AuLcNonDir_DIINFO;
221                 if (S_ISLNK(inode->i_mode))
222                         lc_idx = AuLcSymlink_DIINFO;
223                 else if (S_ISDIR(inode->i_mode))
224                         lc_idx = AuLcDir_DIINFO;
225                 au_rw_class(&au_di(dentry)->di_rwsem, au_lc_key + lc_idx);
226         }
227 out_si:
228         si_read_unlock(sb);
229 out:
230         return ret;
231 }
232
233 /* ---------------------------------------------------------------------- */
234
235 static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
236                           const unsigned char add_entry, aufs_bindex_t bcpup,
237                           aufs_bindex_t bstart)
238 {
239         int err;
240         struct dentry *h_parent;
241         struct inode *h_dir;
242
243         if (add_entry)
244                 IMustLock(parent->d_inode);
245         else
246                 di_write_lock_parent(parent);
247
248         err = 0;
249         if (!au_h_dptr(parent, bcpup)) {
250                 if (bstart < bcpup)
251                         err = au_cpdown_dirs(dentry, bcpup);
252                 else
253                         err = au_cpup_dirs(dentry, bcpup);
254         }
255         if (!err && add_entry) {
256                 h_parent = au_h_dptr(parent, bcpup);
257                 h_dir = h_parent->d_inode;
258                 mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
259                 err = au_lkup_neg(dentry, bcpup);
260                 /* todo: no unlock here */
261                 mutex_unlock(&h_dir->i_mutex);
262
263                 AuDbg("bcpup %d\n", bcpup);
264                 if (!err) {
265                         if (!dentry->d_inode)
266                                 au_set_h_dptr(dentry, bstart, NULL);
267                         au_update_dbrange(dentry, /*do_put_zero*/0);
268                 }
269         }
270
271         if (!add_entry)
272                 di_write_unlock(parent);
273         if (!err)
274                 err = bcpup; /* success */
275
276         AuTraceErr(err);
277         return err;
278 }
279
280 /*
281  * decide the branch and the parent dir where we will create a new entry.
282  * returns new bindex or an error.
283  * copyup the parent dir if needed.
284  */
285 int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
286               struct au_wr_dir_args *args)
287 {
288         int err;
289         aufs_bindex_t bcpup, bstart, src_bstart;
290         const unsigned char add_entry = !!au_ftest_wrdir(args->flags,
291                                                          ADD_ENTRY);
292         struct super_block *sb;
293         struct dentry *parent;
294         struct au_sbinfo *sbinfo;
295
296         sb = dentry->d_sb;
297         sbinfo = au_sbi(sb);
298         parent = dget_parent(dentry);
299         bstart = au_dbstart(dentry);
300         bcpup = bstart;
301         if (args->force_btgt < 0) {
302                 if (src_dentry) {
303                         src_bstart = au_dbstart(src_dentry);
304                         if (src_bstart < bstart)
305                                 bcpup = src_bstart;
306                 } else if (add_entry) {
307                         err = AuWbrCreate(sbinfo, dentry,
308                                           au_ftest_wrdir(args->flags, ISDIR));
309                         bcpup = err;
310                 }
311
312                 if (bcpup < 0 || au_test_ro(sb, bcpup, dentry->d_inode)) {
313                         if (add_entry)
314                                 err = AuWbrCopyup(sbinfo, dentry);
315                         else {
316                                 if (!IS_ROOT(dentry)) {
317                                         di_read_lock_parent(parent, !AuLock_IR);
318                                         err = AuWbrCopyup(sbinfo, dentry);
319                                         di_read_unlock(parent, !AuLock_IR);
320                                 } else
321                                         err = AuWbrCopyup(sbinfo, dentry);
322                         }
323                         bcpup = err;
324                         if (unlikely(err < 0))
325                                 goto out;
326                 }
327         } else {
328                 bcpup = args->force_btgt;
329                 AuDebugOn(au_test_ro(sb, bcpup, dentry->d_inode));
330         }
331
332         AuDbg("bstart %d, bcpup %d\n", bstart, bcpup);
333         err = bcpup;
334         if (bcpup == bstart)
335                 goto out; /* success */
336
337         /* copyup the new parent into the branch we process */
338         err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, bstart);
339         if (err >= 0) {
340                 if (!dentry->d_inode) {
341                         au_set_h_dptr(dentry, bstart, NULL);
342                         au_set_dbstart(dentry, bcpup);
343                         au_set_dbend(dentry, bcpup);
344                 }
345                 AuDebugOn(add_entry && !au_h_dptr(dentry, bcpup));
346         }
347
348 out:
349         dput(parent);
350         return err;
351 }
352
353 /* ---------------------------------------------------------------------- */
354
355 struct dentry *au_pinned_h_parent(struct au_pin *pin)
356 {
357         if (pin && pin->parent)
358                 return au_h_dptr(pin->parent, pin->bindex);
359         return NULL;
360 }
361
362 void au_unpin(struct au_pin *p)
363 {
364         if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
365                 mnt_drop_write(p->h_mnt);
366         if (!p->hdir)
367                 return;
368
369         au_hn_imtx_unlock(p->hdir);
370         if (!au_ftest_pin(p->flags, DI_LOCKED))
371                 di_read_unlock(p->parent, AuLock_IR);
372         iput(p->hdir->hi_inode);
373         dput(p->parent);
374         p->parent = NULL;
375         p->hdir = NULL;
376         p->h_mnt = NULL;
377 }
378
379 int au_do_pin(struct au_pin *p)
380 {
381         int err;
382         struct super_block *sb;
383         struct dentry *h_dentry, *h_parent;
384         struct au_branch *br;
385         struct inode *h_dir;
386
387         err = 0;
388         sb = p->dentry->d_sb;
389         br = au_sbr(sb, p->bindex);
390         if (IS_ROOT(p->dentry)) {
391                 if (au_ftest_pin(p->flags, MNT_WRITE)) {
392                         p->h_mnt = br->br_mnt;
393                         err = mnt_want_write(p->h_mnt);
394                         if (unlikely(err)) {
395                                 au_fclr_pin(p->flags, MNT_WRITE);
396                                 goto out_err;
397                         }
398                 }
399                 goto out;
400         }
401
402         h_dentry = NULL;
403         if (p->bindex <= au_dbend(p->dentry))
404                 h_dentry = au_h_dptr(p->dentry, p->bindex);
405
406         p->parent = dget_parent(p->dentry);
407         if (!au_ftest_pin(p->flags, DI_LOCKED))
408                 di_read_lock(p->parent, AuLock_IR, p->lsc_di);
409
410         h_dir = NULL;
411         h_parent = au_h_dptr(p->parent, p->bindex);
412         p->hdir = au_hi(p->parent->d_inode, p->bindex);
413         if (p->hdir)
414                 h_dir = p->hdir->hi_inode;
415
416         /*
417          * udba case, or
418          * if DI_LOCKED is not set, then p->parent may be different
419          * and h_parent can be NULL.
420          */
421         if (unlikely(!p->hdir || !h_dir || !h_parent)) {
422                 err = -EBUSY;
423                 if (!au_ftest_pin(p->flags, DI_LOCKED))
424                         di_read_unlock(p->parent, AuLock_IR);
425                 dput(p->parent);
426                 p->parent = NULL;
427                 goto out_err;
428         }
429
430         au_igrab(h_dir);
431         au_hn_imtx_lock_nested(p->hdir, p->lsc_hi);
432
433         if (unlikely(p->hdir->hi_inode != h_parent->d_inode)) {
434                 err = -EBUSY;
435                 goto out_unpin;
436         }
437         if (h_dentry) {
438                 err = au_h_verify(h_dentry, p->udba, h_dir, h_parent, br);
439                 if (unlikely(err)) {
440                         au_fclr_pin(p->flags, MNT_WRITE);
441                         goto out_unpin;
442                 }
443         }
444
445         if (au_ftest_pin(p->flags, MNT_WRITE)) {
446                 p->h_mnt = br->br_mnt;
447                 err = mnt_want_write(p->h_mnt);
448                 if (unlikely(err)) {
449                         au_fclr_pin(p->flags, MNT_WRITE);
450                         goto out_unpin;
451                 }
452         }
453         goto out; /* success */
454
455 out_unpin:
456         au_unpin(p);
457 out_err:
458         pr_err("err %d\n", err);
459         err = au_busy_or_stale();
460 out:
461         return err;
462 }
463
464 void au_pin_init(struct au_pin *p, struct dentry *dentry,
465                  aufs_bindex_t bindex, int lsc_di, int lsc_hi,
466                  unsigned int udba, unsigned char flags)
467 {
468         p->dentry = dentry;
469         p->udba = udba;
470         p->lsc_di = lsc_di;
471         p->lsc_hi = lsc_hi;
472         p->flags = flags;
473         p->bindex = bindex;
474
475         p->parent = NULL;
476         p->hdir = NULL;
477         p->h_mnt = NULL;
478 }
479
480 int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
481            unsigned int udba, unsigned char flags)
482 {
483         au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
484                     udba, flags);
485         return au_do_pin(pin);
486 }
487
488 /* ---------------------------------------------------------------------- */
489
490 /*
491  * ->setattr() and ->getattr() are called in various cases.
492  * chmod, stat: dentry is revalidated.
493  * fchmod, fstat: file and dentry are not revalidated, additionally they may be
494  *                unhashed.
495  * for ->setattr(), ia->ia_file is passed from ftruncate only.
496  */
497 /* todo: consolidate with do_refresh() and simple_reval_dpath() */
498 static int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
499 {
500         int err;
501         struct inode *inode;
502         struct dentry *parent;
503
504         err = 0;
505         inode = dentry->d_inode;
506         if (au_digen_test(dentry, sigen)) {
507                 parent = dget_parent(dentry);
508                 di_read_lock_parent(parent, AuLock_IR);
509                 err = au_refresh_dentry(dentry, parent);
510                 di_read_unlock(parent, AuLock_IR);
511                 dput(parent);
512         }
513
514         AuTraceErr(err);
515         return err;
516 }
517
518 #define AuIcpup_DID_CPUP        1
519 #define au_ftest_icpup(flags, name)     ((flags) & AuIcpup_##name)
520 #define au_fset_icpup(flags, name) \
521         do { (flags) |= AuIcpup_##name; } while (0)
522 #define au_fclr_icpup(flags, name) \
523         do { (flags) &= ~AuIcpup_##name; } while (0)
524
525 struct au_icpup_args {
526         unsigned char flags;
527         unsigned char pin_flags;
528         aufs_bindex_t btgt;
529         unsigned int udba;
530         struct au_pin pin;
531         struct path h_path;
532         struct inode *h_inode;
533 };
534
535 static int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
536                             struct au_icpup_args *a)
537 {
538         int err;
539         loff_t sz;
540         aufs_bindex_t bstart, ibstart;
541         struct dentry *hi_wh, *parent;
542         struct inode *inode;
543         struct file *h_file;
544         struct au_wr_dir_args wr_dir_args = {
545                 .force_btgt     = -1,
546                 .flags          = 0
547         };
548
549         bstart = au_dbstart(dentry);
550         inode = dentry->d_inode;
551         if (S_ISDIR(inode->i_mode))
552                 au_fset_wrdir(wr_dir_args.flags, ISDIR);
553         /* plink or hi_wh() case */
554         ibstart = au_ibstart(inode);
555         if (bstart != ibstart && !au_test_ro(inode->i_sb, ibstart, inode))
556                 wr_dir_args.force_btgt = ibstart;
557         err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
558         if (unlikely(err < 0))
559                 goto out;
560         a->btgt = err;
561         if (err != bstart)
562                 au_fset_icpup(a->flags, DID_CPUP);
563
564         err = 0;
565         a->pin_flags = AuPin_MNT_WRITE;
566         parent = NULL;
567         if (!IS_ROOT(dentry)) {
568                 au_fset_pin(a->pin_flags, DI_LOCKED);
569                 parent = dget_parent(dentry);
570                 di_write_lock_parent(parent);
571         }
572
573         err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
574         if (unlikely(err))
575                 goto out_parent;
576
577         a->h_path.dentry = au_h_dptr(dentry, bstart);
578         a->h_inode = a->h_path.dentry->d_inode;
579         mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
580         sz = -1;
581         if ((ia->ia_valid & ATTR_SIZE) && ia->ia_size < i_size_read(a->h_inode))
582                 sz = ia->ia_size;
583
584         h_file = NULL;
585         hi_wh = NULL;
586         if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
587                 hi_wh = au_hi_wh(inode, a->btgt);
588                 if (!hi_wh) {
589                         err = au_sio_cpup_wh(dentry, a->btgt, sz, /*file*/NULL);
590                         if (unlikely(err))
591                                 goto out_unlock;
592                         hi_wh = au_hi_wh(inode, a->btgt);
593                         /* todo: revalidate hi_wh? */
594                 }
595         }
596
597         if (parent) {
598                 au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
599                 di_downgrade_lock(parent, AuLock_IR);
600                 dput(parent);
601                 parent = NULL;
602         }
603         if (!au_ftest_icpup(a->flags, DID_CPUP))
604                 goto out; /* success */
605
606         if (!d_unhashed(dentry)) {
607                 h_file = au_h_open_pre(dentry, bstart);
608                 if (IS_ERR(h_file)) {
609                         err = PTR_ERR(h_file);
610                         h_file = NULL;
611                 } else
612                         err = au_sio_cpup_simple(dentry, a->btgt, sz,
613                                                  AuCpup_DTIME);
614                 if (!err)
615                         a->h_path.dentry = au_h_dptr(dentry, a->btgt);
616         } else if (!hi_wh)
617                 a->h_path.dentry = au_h_dptr(dentry, a->btgt);
618         else
619                 a->h_path.dentry = hi_wh; /* do not dget here */
620
621 out_unlock:
622         mutex_unlock(&a->h_inode->i_mutex);
623         au_h_open_post(dentry, bstart, h_file);
624         a->h_inode = a->h_path.dentry->d_inode;
625         if (!err) {
626                 mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
627                 goto out; /* success */
628         }
629
630         au_unpin(&a->pin);
631 out_parent:
632         if (parent) {
633                 di_write_unlock(parent);
634                 dput(parent);
635         }
636 out:
637         return err;
638 }
639
640 static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
641 {
642         int err;
643         struct inode *inode;
644         struct super_block *sb;
645         struct file *file;
646         struct au_icpup_args *a;
647
648         inode = dentry->d_inode;
649         IMustLock(inode);
650
651         err = -ENOMEM;
652         a = kzalloc(sizeof(*a), GFP_NOFS);
653         if (unlikely(!a))
654                 goto out;
655
656         if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
657                 ia->ia_valid &= ~ATTR_MODE;
658
659         file = NULL;
660         sb = dentry->d_sb;
661         err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
662         if (unlikely(err))
663                 goto out_kfree;
664
665         if (ia->ia_valid & ATTR_FILE) {
666                 /* currently ftruncate(2) only */
667                 AuDebugOn(!S_ISREG(inode->i_mode));
668                 file = ia->ia_file;
669                 err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
670                 if (unlikely(err))
671                         goto out_si;
672                 ia->ia_file = au_hf_top(file);
673                 a->udba = AuOpt_UDBA_NONE;
674         } else {
675                 /* fchmod() doesn't pass ia_file */
676                 a->udba = au_opt_udba(sb);
677                 di_write_lock_child(dentry);
678                 /* no d_unlinked(), to set UDBA_NONE for root */
679                 if (d_unhashed(dentry))
680                         a->udba = AuOpt_UDBA_NONE;
681                 if (a->udba != AuOpt_UDBA_NONE) {
682                         AuDebugOn(IS_ROOT(dentry));
683                         err = au_reval_for_attr(dentry, au_sigen(sb));
684                         if (unlikely(err))
685                                 goto out_dentry;
686                 }
687         }
688
689         err = au_pin_and_icpup(dentry, ia, a);
690         if (unlikely(err < 0))
691                 goto out_dentry;
692         if (au_ftest_icpup(a->flags, DID_CPUP)) {
693                 ia->ia_file = NULL;
694                 ia->ia_valid &= ~ATTR_FILE;
695         }
696
697         a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
698         if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
699             == (ATTR_MODE | ATTR_CTIME)) {
700                 err = security_path_chmod(a->h_path.dentry, a->h_path.mnt,
701                                           ia->ia_mode);
702                 if (unlikely(err))
703                         goto out_unlock;
704         } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
705                    && (ia->ia_valid & ATTR_CTIME)) {
706                 err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
707                 if (unlikely(err))
708                         goto out_unlock;
709         }
710
711         if (ia->ia_valid & ATTR_SIZE) {
712                 struct file *f;
713
714                 if (ia->ia_size < i_size_read(inode))
715                         /* unmap only */
716                         truncate_setsize(inode, ia->ia_size);
717
718                 f = NULL;
719                 if (ia->ia_valid & ATTR_FILE)
720                         f = ia->ia_file;
721                 mutex_unlock(&a->h_inode->i_mutex);
722                 err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
723                 mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
724         } else
725                 err = vfsub_notify_change(&a->h_path, ia);
726         if (!err)
727                 au_cpup_attr_changeable(inode);
728
729 out_unlock:
730         mutex_unlock(&a->h_inode->i_mutex);
731         au_unpin(&a->pin);
732         if (unlikely(err))
733                 au_update_dbstart(dentry);
734 out_dentry:
735         di_write_unlock(dentry);
736         if (file) {
737                 fi_write_unlock(file);
738                 ia->ia_file = file;
739                 ia->ia_valid |= ATTR_FILE;
740         }
741 out_si:
742         si_read_unlock(sb);
743 out_kfree:
744         kfree(a);
745 out:
746         AuTraceErr(err);
747         return err;
748 }
749
750 static void au_refresh_iattr(struct inode *inode, struct kstat *st,
751                              unsigned int nlink)
752 {
753         unsigned int n;
754
755         inode->i_mode = st->mode;
756         inode->i_uid = st->uid;
757         inode->i_gid = st->gid;
758         inode->i_atime = st->atime;
759         inode->i_mtime = st->mtime;
760         inode->i_ctime = st->ctime;
761
762         au_cpup_attr_nlink(inode, /*force*/0);
763         if (S_ISDIR(inode->i_mode)) {
764                 n = inode->i_nlink;
765                 n -= nlink;
766                 n += st->nlink;
767                 set_nlink(inode, n);
768         }
769
770         spin_lock(&inode->i_lock);
771         inode->i_blocks = st->blocks;
772         i_size_write(inode, st->size);
773         spin_unlock(&inode->i_lock);
774 }
775
776 static int aufs_getattr(struct vfsmount *mnt __maybe_unused,
777                         struct dentry *dentry, struct kstat *st)
778 {
779         int err;
780         unsigned int mnt_flags;
781         aufs_bindex_t bindex;
782         unsigned char udba_none, positive;
783         struct super_block *sb, *h_sb;
784         struct inode *inode;
785         struct vfsmount *h_mnt;
786         struct dentry *h_dentry;
787
788         sb = dentry->d_sb;
789         inode = dentry->d_inode;
790         err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
791         if (unlikely(err))
792                 goto out;
793         mnt_flags = au_mntflags(sb);
794         udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
795
796         /* support fstat(2) */
797         if (!d_unlinked(dentry) && !udba_none) {
798                 unsigned int sigen = au_sigen(sb);
799                 err = au_digen_test(dentry, sigen);
800                 if (!err) {
801                         di_read_lock_child(dentry, AuLock_IR);
802                         err = au_dbrange_test(dentry);
803                         if (unlikely(err))
804                                 goto out_unlock;
805                 } else {
806                         AuDebugOn(IS_ROOT(dentry));
807                         di_write_lock_child(dentry);
808                         err = au_dbrange_test(dentry);
809                         if (!err)
810                                 err = au_reval_for_attr(dentry, sigen);
811                         di_downgrade_lock(dentry, AuLock_IR);
812                         if (unlikely(err))
813                                 goto out_unlock;
814                 }
815         } else
816                 di_read_lock_child(dentry, AuLock_IR);
817
818         bindex = au_ibstart(inode);
819         h_mnt = au_sbr_mnt(sb, bindex);
820         h_sb = h_mnt->mnt_sb;
821         if (!au_test_fs_bad_iattr(h_sb) && udba_none)
822                 goto out_fill; /* success */
823
824         h_dentry = NULL;
825         if (au_dbstart(dentry) == bindex)
826                 h_dentry = dget(au_h_dptr(dentry, bindex));
827         else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
828                 h_dentry = au_plink_lkup(inode, bindex);
829                 if (IS_ERR(h_dentry))
830                         goto out_fill; /* pretending success */
831         }
832         /* illegally overlapped or something */
833         if (unlikely(!h_dentry))
834                 goto out_fill; /* pretending success */
835
836         positive = !!h_dentry->d_inode;
837         if (positive)
838                 err = vfs_getattr(h_mnt, h_dentry, st);
839         dput(h_dentry);
840         if (!err) {
841                 if (positive)
842                         au_refresh_iattr(inode, st, h_dentry->d_inode->i_nlink);
843                 goto out_fill; /* success */
844         }
845         AuTraceErr(err);
846         goto out_unlock;
847
848 out_fill:
849         generic_fillattr(inode, st);
850 out_unlock:
851         di_read_unlock(dentry, AuLock_IR);
852         si_read_unlock(sb);
853 out:
854         AuTraceErr(err);
855         return err;
856 }
857
858 /* ---------------------------------------------------------------------- */
859
860 static int h_readlink(struct dentry *dentry, int bindex, char __user *buf,
861                       int bufsiz)
862 {
863         int err;
864         struct super_block *sb;
865         struct dentry *h_dentry;
866
867         err = -EINVAL;
868         h_dentry = au_h_dptr(dentry, bindex);
869         if (unlikely(!h_dentry->d_inode->i_op->readlink))
870                 goto out;
871
872         err = security_inode_readlink(h_dentry);
873         if (unlikely(err))
874                 goto out;
875
876         sb = dentry->d_sb;
877         if (!au_test_ro(sb, bindex, dentry->d_inode)) {
878                 vfsub_touch_atime(au_sbr_mnt(sb, bindex), h_dentry);
879                 fsstack_copy_attr_atime(dentry->d_inode, h_dentry->d_inode);
880         }
881         err = h_dentry->d_inode->i_op->readlink(h_dentry, buf, bufsiz);
882
883 out:
884         return err;
885 }
886
887 static int aufs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
888 {
889         int err;
890
891         err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
892         if (unlikely(err))
893                 goto out;
894         err = au_d_hashed_positive(dentry);
895         if (!err)
896                 err = h_readlink(dentry, au_dbstart(dentry), buf, bufsiz);
897         aufs_read_unlock(dentry, AuLock_IR);
898
899 out:
900         return err;
901 }
902
903 static void *aufs_follow_link(struct dentry *dentry, struct nameidata *nd)
904 {
905         int err;
906         mm_segment_t old_fs;
907         union {
908                 char *k;
909                 char __user *u;
910         } buf;
911
912         err = -ENOMEM;
913         buf.k = __getname_gfp(GFP_NOFS);
914         if (unlikely(!buf.k))
915                 goto out;
916
917         err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
918         if (unlikely(err))
919                 goto out_name;
920
921         err = au_d_hashed_positive(dentry);
922         if (!err) {
923                 old_fs = get_fs();
924                 set_fs(KERNEL_DS);
925                 err = h_readlink(dentry, au_dbstart(dentry), buf.u, PATH_MAX);
926                 set_fs(old_fs);
927         }
928         aufs_read_unlock(dentry, AuLock_IR);
929
930         if (err >= 0) {
931                 buf.k[err] = 0;
932                 /* will be freed by put_link */
933                 nd_set_link(nd, buf.k);
934                 return NULL; /* success */
935         }
936
937 out_name:
938         __putname(buf.k);
939 out:
940         path_put(&nd->path);
941         AuTraceErr(err);
942         return ERR_PTR(err);
943 }
944
945 static void aufs_put_link(struct dentry *dentry __maybe_unused,
946                           struct nameidata *nd, void *cookie __maybe_unused)
947 {
948         __putname(nd_get_link(nd));
949 }
950
951 /* ---------------------------------------------------------------------- */
952
953 static void aufs_truncate_range(struct inode *inode __maybe_unused,
954                                 loff_t start __maybe_unused,
955                                 loff_t end __maybe_unused)
956 {
957         AuUnsupport();
958 }
959
960 /* ---------------------------------------------------------------------- */
961
962 struct inode_operations aufs_symlink_iop = {
963         .permission     = aufs_permission,
964         .setattr        = aufs_setattr,
965         .getattr        = aufs_getattr,
966         .readlink       = aufs_readlink,
967         .follow_link    = aufs_follow_link,
968         .put_link       = aufs_put_link
969 };
970
971 struct inode_operations aufs_dir_iop = {
972         .create         = aufs_create,
973         .lookup         = aufs_lookup,
974         .link           = aufs_link,
975         .unlink         = aufs_unlink,
976         .symlink        = aufs_symlink,
977         .mkdir          = aufs_mkdir,
978         .rmdir          = aufs_rmdir,
979         .mknod          = aufs_mknod,
980         .rename         = aufs_rename,
981
982         .permission     = aufs_permission,
983         .setattr        = aufs_setattr,
984         .getattr        = aufs_getattr
985 };
986
987 struct inode_operations aufs_iop = {
988         .permission     = aufs_permission,
989         .setattr        = aufs_setattr,
990         .getattr        = aufs_getattr,
991         .truncate_range = aufs_truncate_range
992 };