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