]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/aufs/file.c
UBUNTU: ubuntu: vbox -- update to 5.2.6-dfsg-5
[mirror_ubuntu-bionic-kernel.git] / fs / aufs / file.c
1 /*
2 * Copyright (C) 2005-2017 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, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19 * handling file/dir, and address_space operation
20 */
21
22 #ifdef CONFIG_AUFS_DEBUG
23 #include <linux/migrate.h>
24 #endif
25 #include <linux/pagemap.h>
26 #include "aufs.h"
27
28 /* drop flags for writing */
29 unsigned int au_file_roflags(unsigned int flags)
30 {
31 flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
32 flags |= O_RDONLY | O_NOATIME;
33 return flags;
34 }
35
36 /* common functions to regular file and dir */
37 struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
38 struct file *file, int force_wr)
39 {
40 struct file *h_file;
41 struct dentry *h_dentry;
42 struct inode *h_inode;
43 struct super_block *sb;
44 struct au_branch *br;
45 struct path h_path;
46 int err;
47
48 /* a race condition can happen between open and unlink/rmdir */
49 h_file = ERR_PTR(-ENOENT);
50 h_dentry = au_h_dptr(dentry, bindex);
51 if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
52 goto out;
53 h_inode = d_inode(h_dentry);
54 spin_lock(&h_dentry->d_lock);
55 err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
56 /* || !d_inode(dentry)->i_nlink */
57 ;
58 spin_unlock(&h_dentry->d_lock);
59 if (unlikely(err))
60 goto out;
61
62 sb = dentry->d_sb;
63 br = au_sbr(sb, bindex);
64 err = au_br_test_oflag(flags, br);
65 h_file = ERR_PTR(err);
66 if (unlikely(err))
67 goto out;
68
69 /* drop flags for writing */
70 if (au_test_ro(sb, bindex, d_inode(dentry))) {
71 if (force_wr && !(flags & O_WRONLY))
72 force_wr = 0;
73 flags = au_file_roflags(flags);
74 if (force_wr) {
75 h_file = ERR_PTR(-EROFS);
76 flags = au_file_roflags(flags);
77 if (unlikely(vfsub_native_ro(h_inode)
78 || IS_APPEND(h_inode)))
79 goto out;
80 flags &= ~O_ACCMODE;
81 flags |= O_WRONLY;
82 }
83 }
84 flags &= ~O_CREAT;
85 au_br_get(br);
86 h_path.dentry = h_dentry;
87 h_path.mnt = au_br_mnt(br);
88 h_file = vfsub_dentry_open(&h_path, flags);
89 if (IS_ERR(h_file))
90 goto out_br;
91
92 if (flags & __FMODE_EXEC) {
93 err = deny_write_access(h_file);
94 if (unlikely(err)) {
95 fput(h_file);
96 h_file = ERR_PTR(err);
97 goto out_br;
98 }
99 }
100 fsnotify_open(h_file);
101 goto out; /* success */
102
103 out_br:
104 au_br_put(br);
105 out:
106 return h_file;
107 }
108
109 static int au_cmoo(struct dentry *dentry)
110 {
111 int err, cmoo, matched;
112 unsigned int udba;
113 struct path h_path;
114 struct au_pin pin;
115 struct au_cp_generic cpg = {
116 .dentry = dentry,
117 .bdst = -1,
118 .bsrc = -1,
119 .len = -1,
120 .pin = &pin,
121 .flags = AuCpup_DTIME | AuCpup_HOPEN
122 };
123 struct inode *delegated;
124 struct super_block *sb;
125 struct au_sbinfo *sbinfo;
126 struct au_fhsm *fhsm;
127 pid_t pid;
128 struct au_branch *br;
129 struct dentry *parent;
130 struct au_hinode *hdir;
131
132 DiMustWriteLock(dentry);
133 IiMustWriteLock(d_inode(dentry));
134
135 err = 0;
136 if (IS_ROOT(dentry))
137 goto out;
138 cpg.bsrc = au_dbtop(dentry);
139 if (!cpg.bsrc)
140 goto out;
141
142 sb = dentry->d_sb;
143 sbinfo = au_sbi(sb);
144 fhsm = &sbinfo->si_fhsm;
145 pid = au_fhsm_pid(fhsm);
146 rcu_read_lock();
147 matched = (pid
148 && (current->pid == pid
149 || rcu_dereference(current->real_parent)->pid == pid));
150 rcu_read_unlock();
151 if (matched)
152 goto out;
153
154 br = au_sbr(sb, cpg.bsrc);
155 cmoo = au_br_cmoo(br->br_perm);
156 if (!cmoo)
157 goto out;
158 if (!d_is_reg(dentry))
159 cmoo &= AuBrAttr_COO_ALL;
160 if (!cmoo)
161 goto out;
162
163 parent = dget_parent(dentry);
164 di_write_lock_parent(parent);
165 err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
166 cpg.bdst = err;
167 if (unlikely(err < 0)) {
168 err = 0; /* there is no upper writable branch */
169 goto out_dgrade;
170 }
171 AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
172
173 /* do not respect the coo attrib for the target branch */
174 err = au_cpup_dirs(dentry, cpg.bdst);
175 if (unlikely(err))
176 goto out_dgrade;
177
178 di_downgrade_lock(parent, AuLock_IR);
179 udba = au_opt_udba(sb);
180 err = au_pin(&pin, dentry, cpg.bdst, udba,
181 AuPin_DI_LOCKED | AuPin_MNT_WRITE);
182 if (unlikely(err))
183 goto out_parent;
184
185 err = au_sio_cpup_simple(&cpg);
186 au_unpin(&pin);
187 if (unlikely(err))
188 goto out_parent;
189 if (!(cmoo & AuBrWAttr_MOO))
190 goto out_parent; /* success */
191
192 err = au_pin(&pin, dentry, cpg.bsrc, udba,
193 AuPin_DI_LOCKED | AuPin_MNT_WRITE);
194 if (unlikely(err))
195 goto out_parent;
196
197 h_path.mnt = au_br_mnt(br);
198 h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
199 hdir = au_hi(d_inode(parent), cpg.bsrc);
200 delegated = NULL;
201 err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
202 au_unpin(&pin);
203 /* todo: keep h_dentry or not? */
204 if (unlikely(err == -EWOULDBLOCK)) {
205 pr_warn("cannot retry for NFSv4 delegation"
206 " for an internal unlink\n");
207 iput(delegated);
208 }
209 if (unlikely(err)) {
210 pr_err("unlink %pd after coo failed (%d), ignored\n",
211 dentry, err);
212 err = 0;
213 }
214 goto out_parent; /* success */
215
216 out_dgrade:
217 di_downgrade_lock(parent, AuLock_IR);
218 out_parent:
219 di_read_unlock(parent, AuLock_IR);
220 dput(parent);
221 out:
222 AuTraceErr(err);
223 return err;
224 }
225
226 int au_do_open(struct file *file, struct au_do_open_args *args)
227 {
228 int err, aopen = args->aopen;
229 struct dentry *dentry;
230 struct au_finfo *finfo;
231
232 if (!aopen)
233 err = au_finfo_init(file, args->fidir);
234 else {
235 lockdep_off();
236 err = au_finfo_init(file, args->fidir);
237 lockdep_on();
238 }
239 if (unlikely(err))
240 goto out;
241
242 dentry = file->f_path.dentry;
243 AuDebugOn(IS_ERR_OR_NULL(dentry));
244 di_write_lock_child(dentry);
245 err = au_cmoo(dentry);
246 di_downgrade_lock(dentry, AuLock_IR);
247 if (!err) {
248 if (!aopen)
249 err = args->open(file, vfsub_file_flags(file), NULL);
250 else {
251 lockdep_off();
252 err = args->open(file, vfsub_file_flags(file), NULL);
253 lockdep_on();
254 }
255 }
256 di_read_unlock(dentry, AuLock_IR);
257
258 finfo = au_fi(file);
259 if (!err) {
260 finfo->fi_file = file;
261 au_hbl_add(&finfo->fi_hlist,
262 &au_sbi(file->f_path.dentry->d_sb)->si_files);
263 }
264 if (!aopen)
265 fi_write_unlock(file);
266 else {
267 lockdep_off();
268 fi_write_unlock(file);
269 lockdep_on();
270 }
271 if (unlikely(err)) {
272 finfo->fi_hdir = NULL;
273 au_finfo_fin(file);
274 }
275
276 out:
277 AuTraceErr(err);
278 return err;
279 }
280
281 int au_reopen_nondir(struct file *file)
282 {
283 int err;
284 aufs_bindex_t btop;
285 struct dentry *dentry;
286 struct file *h_file, *h_file_tmp;
287
288 dentry = file->f_path.dentry;
289 btop = au_dbtop(dentry);
290 h_file_tmp = NULL;
291 if (au_fbtop(file) == btop) {
292 h_file = au_hf_top(file);
293 if (file->f_mode == h_file->f_mode)
294 return 0; /* success */
295 h_file_tmp = h_file;
296 get_file(h_file_tmp);
297 au_set_h_fptr(file, btop, NULL);
298 }
299 AuDebugOn(au_fi(file)->fi_hdir);
300 /*
301 * it can happen
302 * file exists on both of rw and ro
303 * open --> dbtop and fbtop are both 0
304 * prepend a branch as rw, "rw" become ro
305 * remove rw/file
306 * delete the top branch, "rw" becomes rw again
307 * --> dbtop is 1, fbtop is still 0
308 * write --> fbtop is 0 but dbtop is 1
309 */
310 /* AuDebugOn(au_fbtop(file) < btop); */
311
312 h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
313 file, /*force_wr*/0);
314 err = PTR_ERR(h_file);
315 if (IS_ERR(h_file)) {
316 if (h_file_tmp) {
317 au_sbr_get(dentry->d_sb, btop);
318 au_set_h_fptr(file, btop, h_file_tmp);
319 h_file_tmp = NULL;
320 }
321 goto out; /* todo: close all? */
322 }
323
324 err = 0;
325 au_set_fbtop(file, btop);
326 au_set_h_fptr(file, btop, h_file);
327 au_update_figen(file);
328 /* todo: necessary? */
329 /* file->f_ra = h_file->f_ra; */
330
331 out:
332 if (h_file_tmp)
333 fput(h_file_tmp);
334 return err;
335 }
336
337 /* ---------------------------------------------------------------------- */
338
339 static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
340 struct dentry *hi_wh)
341 {
342 int err;
343 aufs_bindex_t btop;
344 struct au_dinfo *dinfo;
345 struct dentry *h_dentry;
346 struct au_hdentry *hdp;
347
348 dinfo = au_di(file->f_path.dentry);
349 AuRwMustWriteLock(&dinfo->di_rwsem);
350
351 btop = dinfo->di_btop;
352 dinfo->di_btop = btgt;
353 hdp = au_hdentry(dinfo, btgt);
354 h_dentry = hdp->hd_dentry;
355 hdp->hd_dentry = hi_wh;
356 err = au_reopen_nondir(file);
357 hdp->hd_dentry = h_dentry;
358 dinfo->di_btop = btop;
359
360 return err;
361 }
362
363 static int au_ready_to_write_wh(struct file *file, loff_t len,
364 aufs_bindex_t bcpup, struct au_pin *pin)
365 {
366 int err;
367 struct inode *inode, *h_inode;
368 struct dentry *h_dentry, *hi_wh;
369 struct au_cp_generic cpg = {
370 .dentry = file->f_path.dentry,
371 .bdst = bcpup,
372 .bsrc = -1,
373 .len = len,
374 .pin = pin
375 };
376
377 au_update_dbtop(cpg.dentry);
378 inode = d_inode(cpg.dentry);
379 h_inode = NULL;
380 if (au_dbtop(cpg.dentry) <= bcpup
381 && au_dbbot(cpg.dentry) >= bcpup) {
382 h_dentry = au_h_dptr(cpg.dentry, bcpup);
383 if (h_dentry && d_is_positive(h_dentry))
384 h_inode = d_inode(h_dentry);
385 }
386 hi_wh = au_hi_wh(inode, bcpup);
387 if (!hi_wh && !h_inode)
388 err = au_sio_cpup_wh(&cpg, file);
389 else
390 /* already copied-up after unlink */
391 err = au_reopen_wh(file, bcpup, hi_wh);
392
393 if (!err
394 && (inode->i_nlink > 1
395 || (inode->i_state & I_LINKABLE))
396 && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
397 au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
398
399 return err;
400 }
401
402 /*
403 * prepare the @file for writing.
404 */
405 int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
406 {
407 int err;
408 aufs_bindex_t dbtop;
409 struct dentry *parent;
410 struct inode *inode;
411 struct super_block *sb;
412 struct file *h_file;
413 struct au_cp_generic cpg = {
414 .dentry = file->f_path.dentry,
415 .bdst = -1,
416 .bsrc = -1,
417 .len = len,
418 .pin = pin,
419 .flags = AuCpup_DTIME
420 };
421
422 sb = cpg.dentry->d_sb;
423 inode = d_inode(cpg.dentry);
424 cpg.bsrc = au_fbtop(file);
425 err = au_test_ro(sb, cpg.bsrc, inode);
426 if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
427 err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
428 /*flags*/0);
429 goto out;
430 }
431
432 /* need to cpup or reopen */
433 parent = dget_parent(cpg.dentry);
434 di_write_lock_parent(parent);
435 err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
436 cpg.bdst = err;
437 if (unlikely(err < 0))
438 goto out_dgrade;
439 err = 0;
440
441 if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
442 err = au_cpup_dirs(cpg.dentry, cpg.bdst);
443 if (unlikely(err))
444 goto out_dgrade;
445 }
446
447 err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
448 AuPin_DI_LOCKED | AuPin_MNT_WRITE);
449 if (unlikely(err))
450 goto out_dgrade;
451
452 dbtop = au_dbtop(cpg.dentry);
453 if (dbtop <= cpg.bdst)
454 cpg.bsrc = cpg.bdst;
455
456 if (dbtop <= cpg.bdst /* just reopen */
457 || !d_unhashed(cpg.dentry) /* copyup and reopen */
458 ) {
459 h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
460 if (IS_ERR(h_file))
461 err = PTR_ERR(h_file);
462 else {
463 di_downgrade_lock(parent, AuLock_IR);
464 if (dbtop > cpg.bdst)
465 err = au_sio_cpup_simple(&cpg);
466 if (!err)
467 err = au_reopen_nondir(file);
468 au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
469 }
470 } else { /* copyup as wh and reopen */
471 /*
472 * since writable hfsplus branch is not supported,
473 * h_open_pre/post() are unnecessary.
474 */
475 err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
476 di_downgrade_lock(parent, AuLock_IR);
477 }
478
479 if (!err) {
480 au_pin_set_parent_lflag(pin, /*lflag*/0);
481 goto out_dput; /* success */
482 }
483 au_unpin(pin);
484 goto out_unlock;
485
486 out_dgrade:
487 di_downgrade_lock(parent, AuLock_IR);
488 out_unlock:
489 di_read_unlock(parent, AuLock_IR);
490 out_dput:
491 dput(parent);
492 out:
493 return err;
494 }
495
496 /* ---------------------------------------------------------------------- */
497
498 int au_do_flush(struct file *file, fl_owner_t id,
499 int (*flush)(struct file *file, fl_owner_t id))
500 {
501 int err;
502 struct super_block *sb;
503 struct inode *inode;
504
505 inode = file_inode(file);
506 sb = inode->i_sb;
507 si_noflush_read_lock(sb);
508 fi_read_lock(file);
509 ii_read_lock_child(inode);
510
511 err = flush(file, id);
512 au_cpup_attr_timesizes(inode);
513
514 ii_read_unlock(inode);
515 fi_read_unlock(file);
516 si_read_unlock(sb);
517 return err;
518 }
519
520 /* ---------------------------------------------------------------------- */
521
522 static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
523 {
524 int err;
525 struct au_pin pin;
526 struct au_finfo *finfo;
527 struct dentry *parent, *hi_wh;
528 struct inode *inode;
529 struct super_block *sb;
530 struct au_cp_generic cpg = {
531 .dentry = file->f_path.dentry,
532 .bdst = -1,
533 .bsrc = -1,
534 .len = -1,
535 .pin = &pin,
536 .flags = AuCpup_DTIME
537 };
538
539 FiMustWriteLock(file);
540
541 err = 0;
542 finfo = au_fi(file);
543 sb = cpg.dentry->d_sb;
544 inode = d_inode(cpg.dentry);
545 cpg.bdst = au_ibtop(inode);
546 if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
547 goto out;
548
549 parent = dget_parent(cpg.dentry);
550 if (au_test_ro(sb, cpg.bdst, inode)) {
551 di_read_lock_parent(parent, !AuLock_IR);
552 err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
553 cpg.bdst = err;
554 di_read_unlock(parent, !AuLock_IR);
555 if (unlikely(err < 0))
556 goto out_parent;
557 err = 0;
558 }
559
560 di_read_lock_parent(parent, AuLock_IR);
561 hi_wh = au_hi_wh(inode, cpg.bdst);
562 if (!S_ISDIR(inode->i_mode)
563 && au_opt_test(au_mntflags(sb), PLINK)
564 && au_plink_test(inode)
565 && !d_unhashed(cpg.dentry)
566 && cpg.bdst < au_dbtop(cpg.dentry)) {
567 err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
568 if (unlikely(err))
569 goto out_unlock;
570
571 /* always superio. */
572 err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
573 AuPin_DI_LOCKED | AuPin_MNT_WRITE);
574 if (!err) {
575 err = au_sio_cpup_simple(&cpg);
576 au_unpin(&pin);
577 }
578 } else if (hi_wh) {
579 /* already copied-up after unlink */
580 err = au_reopen_wh(file, cpg.bdst, hi_wh);
581 *need_reopen = 0;
582 }
583
584 out_unlock:
585 di_read_unlock(parent, AuLock_IR);
586 out_parent:
587 dput(parent);
588 out:
589 return err;
590 }
591
592 static void au_do_refresh_dir(struct file *file)
593 {
594 aufs_bindex_t bindex, bbot, new_bindex, brid;
595 struct au_hfile *p, tmp, *q;
596 struct au_finfo *finfo;
597 struct super_block *sb;
598 struct au_fidir *fidir;
599
600 FiMustWriteLock(file);
601
602 sb = file->f_path.dentry->d_sb;
603 finfo = au_fi(file);
604 fidir = finfo->fi_hdir;
605 AuDebugOn(!fidir);
606 p = fidir->fd_hfile + finfo->fi_btop;
607 brid = p->hf_br->br_id;
608 bbot = fidir->fd_bbot;
609 for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
610 if (!p->hf_file)
611 continue;
612
613 new_bindex = au_br_index(sb, p->hf_br->br_id);
614 if (new_bindex == bindex)
615 continue;
616 if (new_bindex < 0) {
617 au_set_h_fptr(file, bindex, NULL);
618 continue;
619 }
620
621 /* swap two lower inode, and loop again */
622 q = fidir->fd_hfile + new_bindex;
623 tmp = *q;
624 *q = *p;
625 *p = tmp;
626 if (tmp.hf_file) {
627 bindex--;
628 p--;
629 }
630 }
631
632 p = fidir->fd_hfile;
633 if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
634 bbot = au_sbbot(sb);
635 for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
636 finfo->fi_btop++, p++)
637 if (p->hf_file) {
638 if (file_inode(p->hf_file))
639 break;
640 au_hfput(p, /*execed*/0);
641 }
642 } else {
643 bbot = au_br_index(sb, brid);
644 for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
645 finfo->fi_btop++, p++)
646 if (p->hf_file)
647 au_hfput(p, /*execed*/0);
648 bbot = au_sbbot(sb);
649 }
650
651 p = fidir->fd_hfile + bbot;
652 for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
653 fidir->fd_bbot--, p--)
654 if (p->hf_file) {
655 if (file_inode(p->hf_file))
656 break;
657 au_hfput(p, /*execed*/0);
658 }
659 AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
660 }
661
662 /*
663 * after branch manipulating, refresh the file.
664 */
665 static int refresh_file(struct file *file, int (*reopen)(struct file *file))
666 {
667 int err, need_reopen, nbr;
668 aufs_bindex_t bbot, bindex;
669 struct dentry *dentry;
670 struct super_block *sb;
671 struct au_finfo *finfo;
672 struct au_hfile *hfile;
673
674 dentry = file->f_path.dentry;
675 sb = dentry->d_sb;
676 nbr = au_sbbot(sb) + 1;
677 finfo = au_fi(file);
678 if (!finfo->fi_hdir) {
679 hfile = &finfo->fi_htop;
680 AuDebugOn(!hfile->hf_file);
681 bindex = au_br_index(sb, hfile->hf_br->br_id);
682 AuDebugOn(bindex < 0);
683 if (bindex != finfo->fi_btop)
684 au_set_fbtop(file, bindex);
685 } else {
686 err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
687 if (unlikely(err))
688 goto out;
689 au_do_refresh_dir(file);
690 }
691
692 err = 0;
693 need_reopen = 1;
694 if (!au_test_mmapped(file))
695 err = au_file_refresh_by_inode(file, &need_reopen);
696 if (finfo->fi_hdir)
697 /* harmless if err */
698 au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
699 if (!err && need_reopen && !d_unlinked(dentry))
700 err = reopen(file);
701 if (!err) {
702 au_update_figen(file);
703 goto out; /* success */
704 }
705
706 /* error, close all lower files */
707 if (finfo->fi_hdir) {
708 bbot = au_fbbot_dir(file);
709 for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
710 au_set_h_fptr(file, bindex, NULL);
711 }
712
713 out:
714 return err;
715 }
716
717 /* common function to regular file and dir */
718 int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
719 int wlock, unsigned int fi_lsc)
720 {
721 int err;
722 unsigned int sigen, figen;
723 aufs_bindex_t btop;
724 unsigned char pseudo_link;
725 struct dentry *dentry;
726 struct inode *inode;
727
728 err = 0;
729 dentry = file->f_path.dentry;
730 inode = d_inode(dentry);
731 sigen = au_sigen(dentry->d_sb);
732 fi_write_lock_nested(file, fi_lsc);
733 figen = au_figen(file);
734 if (!fi_lsc)
735 di_write_lock_child(dentry);
736 else
737 di_write_lock_child2(dentry);
738 btop = au_dbtop(dentry);
739 pseudo_link = (btop != au_ibtop(inode));
740 if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
741 if (!wlock) {
742 di_downgrade_lock(dentry, AuLock_IR);
743 fi_downgrade_lock(file);
744 }
745 goto out; /* success */
746 }
747
748 AuDbg("sigen %d, figen %d\n", sigen, figen);
749 if (au_digen_test(dentry, sigen)) {
750 err = au_reval_dpath(dentry, sigen);
751 AuDebugOn(!err && au_digen_test(dentry, sigen));
752 }
753
754 if (!err)
755 err = refresh_file(file, reopen);
756 if (!err) {
757 if (!wlock) {
758 di_downgrade_lock(dentry, AuLock_IR);
759 fi_downgrade_lock(file);
760 }
761 } else {
762 di_write_unlock(dentry);
763 fi_write_unlock(file);
764 }
765
766 out:
767 return err;
768 }
769
770 /* ---------------------------------------------------------------------- */
771
772 /* cf. aufs_nopage() */
773 /* for madvise(2) */
774 static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
775 {
776 unlock_page(page);
777 return 0;
778 }
779
780 /* it will never be called, but necessary to support O_DIRECT */
781 static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
782 { BUG(); return 0; }
783
784 /* they will never be called. */
785 #ifdef CONFIG_AUFS_DEBUG
786 static int aufs_write_begin(struct file *file, struct address_space *mapping,
787 loff_t pos, unsigned len, unsigned flags,
788 struct page **pagep, void **fsdata)
789 { AuUnsupport(); return 0; }
790 static int aufs_write_end(struct file *file, struct address_space *mapping,
791 loff_t pos, unsigned len, unsigned copied,
792 struct page *page, void *fsdata)
793 { AuUnsupport(); return 0; }
794 static int aufs_writepage(struct page *page, struct writeback_control *wbc)
795 { AuUnsupport(); return 0; }
796
797 static int aufs_set_page_dirty(struct page *page)
798 { AuUnsupport(); return 0; }
799 static void aufs_invalidatepage(struct page *page, unsigned int offset,
800 unsigned int length)
801 { AuUnsupport(); }
802 static int aufs_releasepage(struct page *page, gfp_t gfp)
803 { AuUnsupport(); return 0; }
804 #if 0 /* called by memory compaction regardless file */
805 static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
806 struct page *page, enum migrate_mode mode)
807 { AuUnsupport(); return 0; }
808 #endif
809 static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
810 { AuUnsupport(); return true; }
811 static void aufs_putback_page(struct page *page)
812 { AuUnsupport(); }
813 static int aufs_launder_page(struct page *page)
814 { AuUnsupport(); return 0; }
815 static int aufs_is_partially_uptodate(struct page *page,
816 unsigned long from,
817 unsigned long count)
818 { AuUnsupport(); return 0; }
819 static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
820 bool *writeback)
821 { AuUnsupport(); }
822 static int aufs_error_remove_page(struct address_space *mapping,
823 struct page *page)
824 { AuUnsupport(); return 0; }
825 static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
826 sector_t *span)
827 { AuUnsupport(); return 0; }
828 static void aufs_swap_deactivate(struct file *file)
829 { AuUnsupport(); }
830 #endif /* CONFIG_AUFS_DEBUG */
831
832 const struct address_space_operations aufs_aop = {
833 .readpage = aufs_readpage,
834 .direct_IO = aufs_direct_IO,
835 #ifdef CONFIG_AUFS_DEBUG
836 .writepage = aufs_writepage,
837 /* no writepages, because of writepage */
838 .set_page_dirty = aufs_set_page_dirty,
839 /* no readpages, because of readpage */
840 .write_begin = aufs_write_begin,
841 .write_end = aufs_write_end,
842 /* no bmap, no block device */
843 .invalidatepage = aufs_invalidatepage,
844 .releasepage = aufs_releasepage,
845 /* is fallback_migrate_page ok? */
846 /* .migratepage = aufs_migratepage, */
847 .isolate_page = aufs_isolate_page,
848 .putback_page = aufs_putback_page,
849 .launder_page = aufs_launder_page,
850 .is_partially_uptodate = aufs_is_partially_uptodate,
851 .is_dirty_writeback = aufs_is_dirty_writeback,
852 .error_remove_page = aufs_error_remove_page,
853 .swap_activate = aufs_swap_activate,
854 .swap_deactivate = aufs_swap_deactivate
855 #endif /* CONFIG_AUFS_DEBUG */
856 };