7bf2672a9cbc351e0625b5471901a330a560608c
[pandora-kernel.git] / fs / aufs / cpup.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  * copy-up functions, see wbr_policy.c for copy-down
21  */
22
23 #include <linux/fs_stack.h>
24 #include <linux/mm.h>
25 #include "aufs.h"
26
27 void au_cpup_attr_flags(struct inode *dst, struct inode *src)
28 {
29         const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
30                 | S_NOATIME | S_NOCMTIME;
31
32         dst->i_flags |= src->i_flags & ~mask;
33         if (au_test_fs_notime(dst->i_sb))
34                 dst->i_flags |= S_NOATIME | S_NOCMTIME;
35 }
36
37 void au_cpup_attr_timesizes(struct inode *inode)
38 {
39         struct inode *h_inode;
40
41         h_inode = au_h_iptr(inode, au_ibstart(inode));
42         fsstack_copy_attr_times(inode, h_inode);
43         fsstack_copy_inode_size(inode, h_inode);
44 }
45
46 void au_cpup_attr_nlink(struct inode *inode, int force)
47 {
48         struct inode *h_inode;
49         struct super_block *sb;
50         aufs_bindex_t bindex, bend;
51
52         sb = inode->i_sb;
53         bindex = au_ibstart(inode);
54         h_inode = au_h_iptr(inode, bindex);
55         if (!force
56             && !S_ISDIR(h_inode->i_mode)
57             && au_opt_test(au_mntflags(sb), PLINK)
58             && au_plink_test(inode))
59                 return;
60
61         set_nlink(inode, h_inode->i_nlink);
62
63         /*
64          * fewer nlink makes find(1) noisy, but larger nlink doesn't.
65          * it may includes whplink directory.
66          */
67         if (S_ISDIR(h_inode->i_mode)) {
68                 bend = au_ibend(inode);
69                 for (bindex++; bindex <= bend; bindex++) {
70                         h_inode = au_h_iptr(inode, bindex);
71                         if (h_inode)
72                                 au_add_nlink(inode, h_inode);
73                 }
74         }
75 }
76
77 void au_cpup_attr_changeable(struct inode *inode)
78 {
79         struct inode *h_inode;
80
81         h_inode = au_h_iptr(inode, au_ibstart(inode));
82         inode->i_mode = h_inode->i_mode;
83         inode->i_uid = h_inode->i_uid;
84         inode->i_gid = h_inode->i_gid;
85         au_cpup_attr_timesizes(inode);
86         au_cpup_attr_flags(inode, h_inode);
87 }
88
89 void au_cpup_igen(struct inode *inode, struct inode *h_inode)
90 {
91         struct au_iinfo *iinfo = au_ii(inode);
92
93         IiMustWriteLock(inode);
94
95         iinfo->ii_higen = h_inode->i_generation;
96         iinfo->ii_hsb1 = h_inode->i_sb;
97 }
98
99 void au_cpup_attr_all(struct inode *inode, int force)
100 {
101         struct inode *h_inode;
102
103         h_inode = au_h_iptr(inode, au_ibstart(inode));
104         au_cpup_attr_changeable(inode);
105         if (inode->i_nlink > 0)
106                 au_cpup_attr_nlink(inode, force);
107         inode->i_rdev = h_inode->i_rdev;
108         inode->i_blkbits = h_inode->i_blkbits;
109         au_cpup_igen(inode, h_inode);
110 }
111
112 /* ---------------------------------------------------------------------- */
113
114 /* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
115
116 /* keep the timestamps of the parent dir when cpup */
117 void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
118                     struct path *h_path)
119 {
120         struct inode *h_inode;
121
122         dt->dt_dentry = dentry;
123         dt->dt_h_path = *h_path;
124         h_inode = h_path->dentry->d_inode;
125         dt->dt_atime = h_inode->i_atime;
126         dt->dt_mtime = h_inode->i_mtime;
127         /* smp_mb(); */
128 }
129
130 void au_dtime_revert(struct au_dtime *dt)
131 {
132         struct iattr attr;
133         int err;
134
135         attr.ia_atime = dt->dt_atime;
136         attr.ia_mtime = dt->dt_mtime;
137         attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
138                 | ATTR_ATIME | ATTR_ATIME_SET;
139
140         err = vfsub_notify_change(&dt->dt_h_path, &attr);
141         if (unlikely(err))
142                 pr_warning("restoring timestamps failed(%d). ignored\n", err);
143 }
144
145 /* ---------------------------------------------------------------------- */
146
147 static noinline_for_stack
148 int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src)
149 {
150         int err, sbits;
151         struct iattr ia;
152         struct path h_path;
153         struct inode *h_isrc, *h_idst;
154
155         h_path.dentry = au_h_dptr(dst, bindex);
156         h_idst = h_path.dentry->d_inode;
157         h_path.mnt = au_sbr_mnt(dst->d_sb, bindex);
158         h_isrc = h_src->d_inode;
159         ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
160                 | ATTR_ATIME | ATTR_MTIME
161                 | ATTR_ATIME_SET | ATTR_MTIME_SET;
162         ia.ia_uid = h_isrc->i_uid;
163         ia.ia_gid = h_isrc->i_gid;
164         ia.ia_atime = h_isrc->i_atime;
165         ia.ia_mtime = h_isrc->i_mtime;
166         if (h_idst->i_mode != h_isrc->i_mode
167             && !S_ISLNK(h_idst->i_mode)) {
168                 ia.ia_valid |= ATTR_MODE;
169                 ia.ia_mode = h_isrc->i_mode;
170         }
171         sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
172         au_cpup_attr_flags(h_idst, h_isrc);
173         err = vfsub_notify_change(&h_path, &ia);
174
175         /* is this nfs only? */
176         if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
177                 ia.ia_valid = ATTR_FORCE | ATTR_MODE;
178                 ia.ia_mode = h_isrc->i_mode;
179                 err = vfsub_notify_change(&h_path, &ia);
180         }
181
182         return err;
183 }
184
185 /* ---------------------------------------------------------------------- */
186
187 static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
188                            char *buf, unsigned long blksize)
189 {
190         int err;
191         size_t sz, rbytes, wbytes;
192         unsigned char all_zero;
193         char *p, *zp;
194         struct mutex *h_mtx;
195         /* reduce stack usage */
196         struct iattr *ia;
197
198         zp = page_address(ZERO_PAGE(0));
199         if (unlikely(!zp))
200                 return -ENOMEM; /* possible? */
201
202         err = 0;
203         all_zero = 0;
204         while (len) {
205                 AuDbg("len %lld\n", len);
206                 sz = blksize;
207                 if (len < blksize)
208                         sz = len;
209
210                 rbytes = 0;
211                 /* todo: signal_pending? */
212                 while (!rbytes || err == -EAGAIN || err == -EINTR) {
213                         rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
214                         err = rbytes;
215                 }
216                 if (unlikely(err < 0))
217                         break;
218
219                 all_zero = 0;
220                 if (len >= rbytes && rbytes == blksize)
221                         all_zero = !memcmp(buf, zp, rbytes);
222                 if (!all_zero) {
223                         wbytes = rbytes;
224                         p = buf;
225                         while (wbytes) {
226                                 size_t b;
227
228                                 b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
229                                 err = b;
230                                 /* todo: signal_pending? */
231                                 if (unlikely(err == -EAGAIN || err == -EINTR))
232                                         continue;
233                                 if (unlikely(err < 0))
234                                         break;
235                                 wbytes -= b;
236                                 p += b;
237                         }
238                 } else {
239                         loff_t res;
240
241                         AuLabel(hole);
242                         res = vfsub_llseek(dst, rbytes, SEEK_CUR);
243                         err = res;
244                         if (unlikely(res < 0))
245                                 break;
246                 }
247                 len -= rbytes;
248                 err = 0;
249         }
250
251         /* the last block may be a hole */
252         if (!err && all_zero) {
253                 AuLabel(last hole);
254
255                 err = 1;
256                 if (au_test_nfs(dst->f_dentry->d_sb)) {
257                         /* nfs requires this step to make last hole */
258                         /* is this only nfs? */
259                         do {
260                                 /* todo: signal_pending? */
261                                 err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
262                         } while (err == -EAGAIN || err == -EINTR);
263                         if (err == 1)
264                                 dst->f_pos--;
265                 }
266
267                 if (err == 1) {
268                         ia = (void *)buf;
269                         ia->ia_size = dst->f_pos;
270                         ia->ia_valid = ATTR_SIZE | ATTR_FILE;
271                         ia->ia_file = dst;
272                         h_mtx = &dst->f_dentry->d_inode->i_mutex;
273                         mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
274                         err = vfsub_notify_change(&dst->f_path, ia);
275                         mutex_unlock(h_mtx);
276                 }
277         }
278
279         return err;
280 }
281
282 int au_copy_file(struct file *dst, struct file *src, loff_t len)
283 {
284         int err;
285         unsigned long blksize;
286         unsigned char do_kfree;
287         char *buf;
288
289         err = -ENOMEM;
290         blksize = dst->f_dentry->d_sb->s_blocksize;
291         if (!blksize || PAGE_SIZE < blksize)
292                 blksize = PAGE_SIZE;
293         AuDbg("blksize %lu\n", blksize);
294         do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
295         if (do_kfree)
296                 buf = kmalloc(blksize, GFP_NOFS);
297         else
298                 buf = (void *)__get_free_page(GFP_NOFS);
299         if (unlikely(!buf))
300                 goto out;
301
302         if (len > (1 << 22))
303                 AuDbg("copying a large file %lld\n", (long long)len);
304
305         src->f_pos = 0;
306         dst->f_pos = 0;
307         err = au_do_copy_file(dst, src, len, buf, blksize);
308         if (do_kfree)
309                 kfree(buf);
310         else
311                 free_page((unsigned long)buf);
312
313 out:
314         return err;
315 }
316
317 /*
318  * to support a sparse file which is opened with O_APPEND,
319  * we need to close the file.
320  */
321 static int au_cp_regular(struct dentry *dentry, aufs_bindex_t bdst,
322                          aufs_bindex_t bsrc, loff_t len)
323 {
324         int err, i;
325         enum { SRC, DST };
326         struct {
327                 aufs_bindex_t bindex;
328                 unsigned int flags;
329                 struct dentry *dentry;
330                 struct file *file;
331                 void *label, *label_file;
332         } *f, file[] = {
333                 {
334                         .bindex = bsrc,
335                         .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
336                         .file = NULL,
337                         .label = &&out,
338                         .label_file = &&out_src
339                 },
340                 {
341                         .bindex = bdst,
342                         .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
343                         .file = NULL,
344                         .label = &&out_src,
345                         .label_file = &&out_dst
346                 }
347         };
348         struct super_block *sb;
349
350         /* bsrc branch can be ro/rw. */
351         sb = dentry->d_sb;
352         f = file;
353         for (i = 0; i < 2; i++, f++) {
354                 f->dentry = au_h_dptr(dentry, f->bindex);
355                 f->file = au_h_open(dentry, f->bindex, f->flags, /*file*/NULL);
356                 err = PTR_ERR(f->file);
357                 if (IS_ERR(f->file))
358                         goto *f->label;
359                 err = -EINVAL;
360                 if (unlikely(!f->file->f_op))
361                         goto *f->label_file;
362         }
363
364         /* try stopping to update while we copyup */
365         IMustLock(file[SRC].dentry->d_inode);
366         err = au_copy_file(file[DST].file, file[SRC].file, len);
367
368 out_dst:
369         fput(file[DST].file);
370         au_sbr_put(sb, file[DST].bindex);
371 out_src:
372         fput(file[SRC].file);
373         au_sbr_put(sb, file[SRC].bindex);
374 out:
375         return err;
376 }
377
378 static int au_do_cpup_regular(struct dentry *dentry, aufs_bindex_t bdst,
379                               aufs_bindex_t bsrc, loff_t len,
380                               struct inode *h_dir, struct path *h_path)
381 {
382         int err, rerr;
383         loff_t l;
384
385         err = 0;
386         l = i_size_read(au_h_iptr(dentry->d_inode, bsrc));
387         if (len == -1 || l < len)
388                 len = l;
389         if (len)
390                 err = au_cp_regular(dentry, bdst, bsrc, len);
391         if (!err)
392                 goto out; /* success */
393
394         rerr = vfsub_unlink(h_dir, h_path, /*force*/0);
395         if (rerr) {
396                 AuIOErr("failed unlinking cpup-ed %.*s(%d, %d)\n",
397                         AuDLNPair(h_path->dentry), err, rerr);
398                 err = -EIO;
399         }
400
401 out:
402         return err;
403 }
404
405 static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
406                               struct inode *h_dir)
407 {
408         int err, symlen;
409         mm_segment_t old_fs;
410         union {
411                 char *k;
412                 char __user *u;
413         } sym;
414
415         err = -ENOSYS;
416         if (unlikely(!h_src->d_inode->i_op->readlink))
417                 goto out;
418
419         err = -ENOMEM;
420         sym.k = __getname_gfp(GFP_NOFS);
421         if (unlikely(!sym.k))
422                 goto out;
423
424         /* unnecessary to support mmap_sem since symlink is not mmap-able */
425         old_fs = get_fs();
426         set_fs(KERNEL_DS);
427         symlen = h_src->d_inode->i_op->readlink(h_src, sym.u, PATH_MAX);
428         err = symlen;
429         set_fs(old_fs);
430
431         if (symlen > 0) {
432                 sym.k[symlen] = 0;
433                 err = vfsub_symlink(h_dir, h_path, sym.k);
434         }
435         __putname(sym.k);
436
437 out:
438         return err;
439 }
440
441 /* return with the lower dst inode is locked */
442 static noinline_for_stack
443 int cpup_entry(struct dentry *dentry, aufs_bindex_t bdst,
444                aufs_bindex_t bsrc, loff_t len, unsigned int flags,
445                struct dentry *dst_parent)
446 {
447         int err;
448         umode_t mode;
449         unsigned int mnt_flags;
450         unsigned char isdir;
451         const unsigned char do_dt = !!au_ftest_cpup(flags, DTIME);
452         struct au_dtime dt;
453         struct path h_path;
454         struct dentry *h_src, *h_dst, *h_parent;
455         struct inode *h_inode, *h_dir;
456         struct super_block *sb;
457
458         /* bsrc branch can be ro/rw. */
459         h_src = au_h_dptr(dentry, bsrc);
460         h_inode = h_src->d_inode;
461         AuDebugOn(h_inode != au_h_iptr(dentry->d_inode, bsrc));
462
463         /* try stopping to be referenced while we are creating */
464         h_dst = au_h_dptr(dentry, bdst);
465         h_parent = h_dst->d_parent; /* dir inode is locked */
466         h_dir = h_parent->d_inode;
467         IMustLock(h_dir);
468         AuDebugOn(h_parent != h_dst->d_parent);
469
470         sb = dentry->d_sb;
471         h_path.mnt = au_sbr_mnt(sb, bdst);
472         if (do_dt) {
473                 h_path.dentry = h_parent;
474                 au_dtime_store(&dt, dst_parent, &h_path);
475         }
476         h_path.dentry = h_dst;
477
478         isdir = 0;
479         mode = h_inode->i_mode;
480         switch (mode & S_IFMT) {
481         case S_IFREG:
482                 /* try stopping to update while we are referencing */
483                 IMustLock(h_inode);
484                 err = vfsub_create(h_dir, &h_path, mode | S_IWUSR);
485                 if (!err)
486                         err = au_do_cpup_regular
487                                 (dentry, bdst, bsrc, len,
488                                  au_h_iptr(dst_parent->d_inode, bdst), &h_path);
489                 break;
490         case S_IFDIR:
491                 isdir = 1;
492                 err = vfsub_mkdir(h_dir, &h_path, mode);
493                 if (!err) {
494                         /*
495                          * strange behaviour from the users view,
496                          * particularry setattr case
497                          */
498                         if (au_ibstart(dst_parent->d_inode) == bdst)
499                                 au_cpup_attr_nlink(dst_parent->d_inode,
500                                                    /*force*/1);
501                         au_cpup_attr_nlink(dentry->d_inode, /*force*/1);
502                 }
503                 break;
504         case S_IFLNK:
505                 err = au_do_cpup_symlink(&h_path, h_src, h_dir);
506                 break;
507         case S_IFCHR:
508         case S_IFBLK:
509                 AuDebugOn(!capable(CAP_MKNOD));
510                 /*FALLTHROUGH*/
511         case S_IFIFO:
512         case S_IFSOCK:
513                 err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
514                 break;
515         default:
516                 AuIOErr("Unknown inode type 0%o\n", mode);
517                 err = -EIO;
518         }
519
520         mnt_flags = au_mntflags(sb);
521         if (!au_opt_test(mnt_flags, UDBA_NONE)
522             && !isdir
523             && au_opt_test(mnt_flags, XINO)
524             && h_inode->i_nlink == 1
525             /* todo: unnecessary? */
526             /* && dentry->d_inode->i_nlink == 1 */
527             && bdst < bsrc
528             && !au_ftest_cpup(flags, KEEPLINO))
529                 au_xino_write(sb, bsrc, h_inode->i_ino, /*ino*/0);
530                 /* ignore this error */
531
532         if (do_dt)
533                 au_dtime_revert(&dt);
534         return err;
535 }
536
537 /*
538  * copyup the @dentry from @bsrc to @bdst.
539  * the caller must set the both of lower dentries.
540  * @len is for truncating when it is -1 copyup the entire file.
541  * in link/rename cases, @dst_parent may be different from the real one.
542  */
543 static int au_cpup_single(struct dentry *dentry, aufs_bindex_t bdst,
544                           aufs_bindex_t bsrc, loff_t len, unsigned int flags,
545                           struct dentry *dst_parent)
546 {
547         int err, rerr;
548         aufs_bindex_t old_ibstart;
549         unsigned char isdir, plink;
550         struct au_dtime dt;
551         struct path h_path;
552         struct dentry *h_src, *h_dst, *h_parent;
553         struct inode *dst_inode, *h_dir, *inode;
554         struct super_block *sb;
555
556         AuDebugOn(bsrc <= bdst);
557
558         sb = dentry->d_sb;
559         h_path.mnt = au_sbr_mnt(sb, bdst);
560         h_dst = au_h_dptr(dentry, bdst);
561         h_parent = h_dst->d_parent; /* dir inode is locked */
562         h_dir = h_parent->d_inode;
563         IMustLock(h_dir);
564
565         h_src = au_h_dptr(dentry, bsrc);
566         inode = dentry->d_inode;
567
568         if (!dst_parent)
569                 dst_parent = dget_parent(dentry);
570         else
571                 dget(dst_parent);
572
573         plink = !!au_opt_test(au_mntflags(sb), PLINK);
574         dst_inode = au_h_iptr(inode, bdst);
575         if (dst_inode) {
576                 if (unlikely(!plink)) {
577                         err = -EIO;
578                         AuIOErr("hi%lu(i%lu) exists on b%d "
579                                 "but plink is disabled\n",
580                                 dst_inode->i_ino, inode->i_ino, bdst);
581                         goto out;
582                 }
583
584                 if (dst_inode->i_nlink) {
585                         const int do_dt = au_ftest_cpup(flags, DTIME);
586
587                         h_src = au_plink_lkup(inode, bdst);
588                         err = PTR_ERR(h_src);
589                         if (IS_ERR(h_src))
590                                 goto out;
591                         if (unlikely(!h_src->d_inode)) {
592                                 err = -EIO;
593                                 AuIOErr("i%lu exists on a upper branch "
594                                         "but not pseudo-linked\n",
595                                         inode->i_ino);
596                                 dput(h_src);
597                                 goto out;
598                         }
599
600                         if (do_dt) {
601                                 h_path.dentry = h_parent;
602                                 au_dtime_store(&dt, dst_parent, &h_path);
603                         }
604                         h_path.dentry = h_dst;
605                         err = vfsub_link(h_src, h_dir, &h_path);
606                         if (do_dt)
607                                 au_dtime_revert(&dt);
608                         dput(h_src);
609                         goto out;
610                 } else
611                         /* todo: cpup_wh_file? */
612                         /* udba work */
613                         au_update_ibrange(inode, /*do_put_zero*/1);
614         }
615
616         old_ibstart = au_ibstart(inode);
617         err = cpup_entry(dentry, bdst, bsrc, len, flags, dst_parent);
618         if (unlikely(err))
619                 goto out;
620         dst_inode = h_dst->d_inode;
621         mutex_lock_nested(&dst_inode->i_mutex, AuLsc_I_CHILD2);
622
623         err = cpup_iattr(dentry, bdst, h_src);
624         isdir = S_ISDIR(dst_inode->i_mode);
625         if (!err) {
626                 if (bdst < old_ibstart) {
627                         if (S_ISREG(inode->i_mode)) {
628                                 err = au_dy_iaop(inode, bdst, dst_inode);
629                                 if (unlikely(err))
630                                         goto out_rev;
631                         }
632                         au_set_ibstart(inode, bdst);
633                 }
634                 au_set_h_iptr(inode, bdst, au_igrab(dst_inode),
635                               au_hi_flags(inode, isdir));
636                 mutex_unlock(&dst_inode->i_mutex);
637                 if (!isdir
638                     && h_src->d_inode->i_nlink > 1
639                     && plink)
640                         au_plink_append(inode, bdst, h_dst);
641                 goto out; /* success */
642         }
643
644         /* revert */
645 out_rev:
646         h_path.dentry = h_parent;
647         mutex_unlock(&dst_inode->i_mutex);
648         au_dtime_store(&dt, dst_parent, &h_path);
649         h_path.dentry = h_dst;
650         if (!isdir)
651                 rerr = vfsub_unlink(h_dir, &h_path, /*force*/0);
652         else
653                 rerr = vfsub_rmdir(h_dir, &h_path);
654         au_dtime_revert(&dt);
655         if (rerr) {
656                 AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
657                 err = -EIO;
658         }
659
660 out:
661         dput(dst_parent);
662         return err;
663 }
664
665 struct au_cpup_single_args {
666         int *errp;
667         struct dentry *dentry;
668         aufs_bindex_t bdst, bsrc;
669         loff_t len;
670         unsigned int flags;
671         struct dentry *dst_parent;
672 };
673
674 static void au_call_cpup_single(void *args)
675 {
676         struct au_cpup_single_args *a = args;
677         *a->errp = au_cpup_single(a->dentry, a->bdst, a->bsrc, a->len,
678                                   a->flags, a->dst_parent);
679 }
680
681 /*
682  * prevent SIGXFSZ in copy-up.
683  * testing CAP_MKNOD is for generic fs,
684  * but CAP_FSETID is for xfs only, currently.
685  */
686 static int au_cpup_sio_test(struct super_block *sb, umode_t mode)
687 {
688         int do_sio;
689
690         do_sio = 0;
691         if (!au_wkq_test()
692             && (!au_sbi(sb)->si_plink_maint_pid
693                 || au_plink_maint(sb, AuLock_NOPLM))) {
694                 switch (mode & S_IFMT) {
695                 case S_IFREG:
696                         /* no condition about RLIMIT_FSIZE and the file size */
697                         do_sio = 1;
698                         break;
699                 case S_IFCHR:
700                 case S_IFBLK:
701                         do_sio = !capable(CAP_MKNOD);
702                         break;
703                 }
704                 if (!do_sio)
705                         do_sio = ((mode & (S_ISUID | S_ISGID))
706                                   && !capable(CAP_FSETID));
707         }
708
709         return do_sio;
710 }
711
712 int au_sio_cpup_single(struct dentry *dentry, aufs_bindex_t bdst,
713                        aufs_bindex_t bsrc, loff_t len, unsigned int flags,
714                        struct dentry *dst_parent)
715 {
716         int err, wkq_err;
717         struct dentry *h_dentry;
718
719         h_dentry = au_h_dptr(dentry, bsrc);
720         if (!au_cpup_sio_test(dentry->d_sb, h_dentry->d_inode->i_mode))
721                 err = au_cpup_single(dentry, bdst, bsrc, len, flags,
722                                      dst_parent);
723         else {
724                 struct au_cpup_single_args args = {
725                         .errp           = &err,
726                         .dentry         = dentry,
727                         .bdst           = bdst,
728                         .bsrc           = bsrc,
729                         .len            = len,
730                         .flags          = flags,
731                         .dst_parent     = dst_parent
732                 };
733                 wkq_err = au_wkq_wait(au_call_cpup_single, &args);
734                 if (unlikely(wkq_err))
735                         err = wkq_err;
736         }
737
738         return err;
739 }
740
741 /*
742  * copyup the @dentry from the first active lower branch to @bdst,
743  * using au_cpup_single().
744  */
745 static int au_cpup_simple(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
746                           unsigned int flags)
747 {
748         int err;
749         aufs_bindex_t bsrc, bend;
750
751         bend = au_dbend(dentry);
752         for (bsrc = bdst + 1; bsrc <= bend; bsrc++)
753                 if (au_h_dptr(dentry, bsrc))
754                         break;
755
756         err = au_lkup_neg(dentry, bdst);
757         if (!err) {
758                 err = au_cpup_single(dentry, bdst, bsrc, len, flags, NULL);
759                 if (!err)
760                         return 0; /* success */
761
762                 /* revert */
763                 au_set_h_dptr(dentry, bdst, NULL);
764                 au_set_dbstart(dentry, bsrc);
765         }
766
767         return err;
768 }
769
770 struct au_cpup_simple_args {
771         int *errp;
772         struct dentry *dentry;
773         aufs_bindex_t bdst;
774         loff_t len;
775         unsigned int flags;
776 };
777
778 static void au_call_cpup_simple(void *args)
779 {
780         struct au_cpup_simple_args *a = args;
781         *a->errp = au_cpup_simple(a->dentry, a->bdst, a->len, a->flags);
782 }
783
784 int au_sio_cpup_simple(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
785                        unsigned int flags)
786 {
787         int err, wkq_err;
788         struct dentry *parent;
789         struct inode *h_dir;
790
791         parent = dget_parent(dentry);
792         h_dir = au_h_iptr(parent->d_inode, bdst);
793         if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
794             && !au_cpup_sio_test(dentry->d_sb, dentry->d_inode->i_mode))
795                 err = au_cpup_simple(dentry, bdst, len, flags);
796         else {
797                 struct au_cpup_simple_args args = {
798                         .errp           = &err,
799                         .dentry         = dentry,
800                         .bdst           = bdst,
801                         .len            = len,
802                         .flags          = flags
803                 };
804                 wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
805                 if (unlikely(wkq_err))
806                         err = wkq_err;
807         }
808
809         dput(parent);
810         return err;
811 }
812
813 /* ---------------------------------------------------------------------- */
814
815 /*
816  * copyup the deleted file for writing.
817  */
818 static int au_do_cpup_wh(struct dentry *dentry, aufs_bindex_t bdst,
819                          struct dentry *wh_dentry, struct file *file,
820                          loff_t len)
821 {
822         int err;
823         aufs_bindex_t bstart;
824         struct au_dinfo *dinfo;
825         struct dentry *h_d_dst, *h_d_start;
826         struct au_hdentry *hdp;
827
828         dinfo = au_di(dentry);
829         AuRwMustWriteLock(&dinfo->di_rwsem);
830
831         bstart = dinfo->di_bstart;
832         hdp = dinfo->di_hdentry;
833         h_d_dst = hdp[0 + bdst].hd_dentry;
834         dinfo->di_bstart = bdst;
835         hdp[0 + bdst].hd_dentry = wh_dentry;
836         if (file) {
837                 h_d_start = hdp[0 + bstart].hd_dentry;
838                 hdp[0 + bstart].hd_dentry = au_hf_top(file)->f_dentry;
839         }
840         err = au_cpup_single(dentry, bdst, bstart, len, !AuCpup_DTIME,
841                              /*h_parent*/NULL);
842         if (file) {
843                 if (!err)
844                         err = au_reopen_nondir(file);
845                 hdp[0 + bstart].hd_dentry = h_d_start;
846         }
847         hdp[0 + bdst].hd_dentry = h_d_dst;
848         dinfo->di_bstart = bstart;
849
850         return err;
851 }
852
853 static int au_cpup_wh(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
854                       struct file *file)
855 {
856         int err;
857         struct au_dtime dt;
858         struct dentry *parent, *h_parent, *wh_dentry;
859         struct au_branch *br;
860         struct path h_path;
861
862         br = au_sbr(dentry->d_sb, bdst);
863         parent = dget_parent(dentry);
864         h_parent = au_h_dptr(parent, bdst);
865         wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
866         err = PTR_ERR(wh_dentry);
867         if (IS_ERR(wh_dentry))
868                 goto out;
869
870         h_path.dentry = h_parent;
871         h_path.mnt = br->br_mnt;
872         au_dtime_store(&dt, parent, &h_path);
873         err = au_do_cpup_wh(dentry, bdst, wh_dentry, file, len);
874         if (unlikely(err))
875                 goto out_wh;
876
877         dget(wh_dentry);
878         h_path.dentry = wh_dentry;
879         if (!S_ISDIR(wh_dentry->d_inode->i_mode))
880                 err = vfsub_unlink(h_parent->d_inode, &h_path, /*force*/0);
881         else
882                 err = vfsub_rmdir(h_parent->d_inode, &h_path);
883         if (unlikely(err)) {
884                 AuIOErr("failed remove copied-up tmp file %.*s(%d)\n",
885                         AuDLNPair(wh_dentry), err);
886                 err = -EIO;
887         }
888         au_dtime_revert(&dt);
889         au_set_hi_wh(dentry->d_inode, bdst, wh_dentry);
890
891 out_wh:
892         dput(wh_dentry);
893 out:
894         dput(parent);
895         return err;
896 }
897
898 struct au_cpup_wh_args {
899         int *errp;
900         struct dentry *dentry;
901         aufs_bindex_t bdst;
902         loff_t len;
903         struct file *file;
904 };
905
906 static void au_call_cpup_wh(void *args)
907 {
908         struct au_cpup_wh_args *a = args;
909         *a->errp = au_cpup_wh(a->dentry, a->bdst, a->len, a->file);
910 }
911
912 int au_sio_cpup_wh(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
913                    struct file *file)
914 {
915         int err, wkq_err;
916         struct dentry *parent, *h_orph, *h_parent, *h_dentry;
917         struct inode *dir, *h_dir, *h_tmpdir, *h_inode;
918         struct au_wbr *wbr;
919
920         parent = dget_parent(dentry);
921         dir = parent->d_inode;
922         h_orph = NULL;
923         h_parent = NULL;
924         h_dir = au_igrab(au_h_iptr(dir, bdst));
925         h_tmpdir = h_dir;
926         if (!h_dir->i_nlink) {
927                 wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
928                 h_orph = wbr->wbr_orph;
929
930                 h_parent = dget(au_h_dptr(parent, bdst));
931                 au_set_h_dptr(parent, bdst, dget(h_orph));
932                 h_tmpdir = h_orph->d_inode;
933                 au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
934
935                 /* this temporary unlock is safe */
936                 if (file)
937                         h_dentry = au_hf_top(file)->f_dentry;
938                 else
939                         h_dentry = au_h_dptr(dentry, au_dbstart(dentry));
940                 h_inode = h_dentry->d_inode;
941                 IMustLock(h_inode);
942                 mutex_unlock(&h_inode->i_mutex);
943                 mutex_lock_nested(&h_tmpdir->i_mutex, AuLsc_I_PARENT3);
944                 mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
945                 /* todo: au_h_open_pre()? */
946         }
947
948         if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
949             && !au_cpup_sio_test(dentry->d_sb, dentry->d_inode->i_mode))
950                 err = au_cpup_wh(dentry, bdst, len, file);
951         else {
952                 struct au_cpup_wh_args args = {
953                         .errp   = &err,
954                         .dentry = dentry,
955                         .bdst   = bdst,
956                         .len    = len,
957                         .file   = file
958                 };
959                 wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
960                 if (unlikely(wkq_err))
961                         err = wkq_err;
962         }
963
964         if (h_orph) {
965                 mutex_unlock(&h_tmpdir->i_mutex);
966                 /* todo: au_h_open_post()? */
967                 au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
968                 au_set_h_dptr(parent, bdst, h_parent);
969         }
970         iput(h_dir);
971         dput(parent);
972
973         return err;
974 }
975
976 /* ---------------------------------------------------------------------- */
977
978 /*
979  * generic routine for both of copy-up and copy-down.
980  */
981 /* cf. revalidate function in file.c */
982 int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
983                int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
984                          struct dentry *h_parent, void *arg),
985                void *arg)
986 {
987         int err;
988         struct au_pin pin;
989         struct dentry *d, *parent, *h_parent, *real_parent;
990
991         err = 0;
992         parent = dget_parent(dentry);
993         if (IS_ROOT(parent))
994                 goto out;
995
996         au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
997                     au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
998
999         /* do not use au_dpage */
1000         real_parent = parent;
1001         while (1) {
1002                 dput(parent);
1003                 parent = dget_parent(dentry);
1004                 h_parent = au_h_dptr(parent, bdst);
1005                 if (h_parent)
1006                         goto out; /* success */
1007
1008                 /* find top dir which is necessary to cpup */
1009                 do {
1010                         d = parent;
1011                         dput(parent);
1012                         parent = dget_parent(d);
1013                         di_read_lock_parent3(parent, !AuLock_IR);
1014                         h_parent = au_h_dptr(parent, bdst);
1015                         di_read_unlock(parent, !AuLock_IR);
1016                 } while (!h_parent);
1017
1018                 if (d != real_parent)
1019                         di_write_lock_child3(d);
1020
1021                 /* somebody else might create while we were sleeping */
1022                 if (!au_h_dptr(d, bdst) || !au_h_dptr(d, bdst)->d_inode) {
1023                         if (au_h_dptr(d, bdst))
1024                                 au_update_dbstart(d);
1025
1026                         au_pin_set_dentry(&pin, d);
1027                         err = au_do_pin(&pin);
1028                         if (!err) {
1029                                 err = cp(d, bdst, h_parent, arg);
1030                                 au_unpin(&pin);
1031                         }
1032                 }
1033
1034                 if (d != real_parent)
1035                         di_write_unlock(d);
1036                 if (unlikely(err))
1037                         break;
1038         }
1039
1040 out:
1041         dput(parent);
1042         return err;
1043 }
1044
1045 static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
1046                        struct dentry *h_parent __maybe_unused ,
1047                        void *arg __maybe_unused)
1048 {
1049         return au_sio_cpup_simple(dentry, bdst, -1, AuCpup_DTIME);
1050 }
1051
1052 int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
1053 {
1054         return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
1055 }
1056
1057 int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
1058 {
1059         int err;
1060         struct dentry *parent;
1061         struct inode *dir;
1062
1063         parent = dget_parent(dentry);
1064         dir = parent->d_inode;
1065         err = 0;
1066         if (au_h_iptr(dir, bdst))
1067                 goto out;
1068
1069         di_read_unlock(parent, AuLock_IR);
1070         di_write_lock_parent(parent);
1071         /* someone else might change our inode while we were sleeping */
1072         if (!au_h_iptr(dir, bdst))
1073                 err = au_cpup_dirs(dentry, bdst);
1074         di_downgrade_lock(parent, AuLock_IR);
1075
1076 out:
1077         dput(parent);
1078         return err;
1079 }