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