]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/aufs/inode.c
UBUNTU: Ubuntu-4.13.0-45.50
[mirror_ubuntu-artful-kernel.git] / fs / aufs / inode.c
CommitLineData
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 * inode functions
20 */
21
22#include "aufs.h"
23
24struct inode *au_igrab(struct inode *inode)
25{
26 if (inode) {
27 AuDebugOn(!atomic_read(&inode->i_count));
28 ihold(inode);
29 }
30 return inode;
31}
32
33static void au_refresh_hinode_attr(struct inode *inode, int do_version)
34{
35 au_cpup_attr_all(inode, /*force*/0);
36 au_update_iigen(inode, /*half*/1);
37 if (do_version)
38 inode->i_version++;
39}
40
41static int au_ii_refresh(struct inode *inode, int *update)
42{
43 int err, e, nbr;
44 umode_t type;
45 aufs_bindex_t bindex, new_bindex;
46 struct super_block *sb;
47 struct au_iinfo *iinfo;
48 struct au_hinode *p, *q, tmp;
49
50 AuDebugOn(au_is_bad_inode(inode));
51 IiMustWriteLock(inode);
52
53 *update = 0;
54 sb = inode->i_sb;
55 nbr = au_sbbot(sb) + 1;
56 type = inode->i_mode & S_IFMT;
57 iinfo = au_ii(inode);
58 err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
59 if (unlikely(err))
60 goto out;
61
62 AuDebugOn(iinfo->ii_btop < 0);
63 p = au_hinode(iinfo, iinfo->ii_btop);
64 for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
65 bindex++, p++) {
66 if (!p->hi_inode)
67 continue;
68
69 AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
70 new_bindex = au_br_index(sb, p->hi_id);
71 if (new_bindex == bindex)
72 continue;
73
74 if (new_bindex < 0) {
75 *update = 1;
76 au_hiput(p);
77 p->hi_inode = NULL;
78 continue;
79 }
80
81 if (new_bindex < iinfo->ii_btop)
82 iinfo->ii_btop = new_bindex;
83 if (iinfo->ii_bbot < new_bindex)
84 iinfo->ii_bbot = new_bindex;
85 /* swap two lower inode, and loop again */
86 q = au_hinode(iinfo, new_bindex);
87 tmp = *q;
88 *q = *p;
89 *p = tmp;
90 if (tmp.hi_inode) {
91 bindex--;
92 p--;
93 }
94 }
95 au_update_ibrange(inode, /*do_put_zero*/0);
96 au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
97 e = au_dy_irefresh(inode);
98 if (unlikely(e && !err))
99 err = e;
100
101out:
102 AuTraceErr(err);
103 return err;
104}
105
106void au_refresh_iop(struct inode *inode, int force_getattr)
107{
108 int type;
109 struct au_sbinfo *sbi = au_sbi(inode->i_sb);
110 const struct inode_operations *iop
111 = force_getattr ? aufs_iop : sbi->si_iop_array;
112
113 if (inode->i_op == iop)
114 return;
115
116 switch (inode->i_mode & S_IFMT) {
117 case S_IFDIR:
118 type = AuIop_DIR;
119 break;
120 case S_IFLNK:
121 type = AuIop_SYMLINK;
122 break;
123 default:
124 type = AuIop_OTHER;
125 break;
126 }
127
128 inode->i_op = iop + type;
129 /* unnecessary smp_wmb() */
130}
131
132int au_refresh_hinode_self(struct inode *inode)
133{
134 int err, update;
135
136 err = au_ii_refresh(inode, &update);
137 if (!err)
138 au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
139
140 AuTraceErr(err);
141 return err;
142}
143
144int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
145{
146 int err, e, update;
147 unsigned int flags;
148 umode_t mode;
149 aufs_bindex_t bindex, bbot;
150 unsigned char isdir;
151 struct au_hinode *p;
152 struct au_iinfo *iinfo;
153
154 err = au_ii_refresh(inode, &update);
155 if (unlikely(err))
156 goto out;
157
158 update = 0;
159 iinfo = au_ii(inode);
160 p = au_hinode(iinfo, iinfo->ii_btop);
161 mode = (inode->i_mode & S_IFMT);
162 isdir = S_ISDIR(mode);
163 flags = au_hi_flags(inode, isdir);
164 bbot = au_dbbot(dentry);
165 for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
166 struct inode *h_i, *h_inode;
167 struct dentry *h_d;
168
169 h_d = au_h_dptr(dentry, bindex);
170 if (!h_d || d_is_negative(h_d))
171 continue;
172
173 h_inode = d_inode(h_d);
174 AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
175 if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
176 h_i = au_h_iptr(inode, bindex);
177 if (h_i) {
178 if (h_i == h_inode)
179 continue;
180 err = -EIO;
181 break;
182 }
183 }
184 if (bindex < iinfo->ii_btop)
185 iinfo->ii_btop = bindex;
186 if (iinfo->ii_bbot < bindex)
187 iinfo->ii_bbot = bindex;
188 au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
189 update = 1;
190 }
191 au_update_ibrange(inode, /*do_put_zero*/0);
192 e = au_dy_irefresh(inode);
193 if (unlikely(e && !err))
194 err = e;
195 if (!err)
196 au_refresh_hinode_attr(inode, update && isdir);
197
198out:
199 AuTraceErr(err);
200 return err;
201}
202
203static int set_inode(struct inode *inode, struct dentry *dentry)
204{
205 int err;
206 unsigned int flags;
207 umode_t mode;
208 aufs_bindex_t bindex, btop, btail;
209 unsigned char isdir;
210 struct dentry *h_dentry;
211 struct inode *h_inode;
212 struct au_iinfo *iinfo;
213 struct inode_operations *iop;
214
215 IiMustWriteLock(inode);
216
217 err = 0;
218 isdir = 0;
219 iop = au_sbi(inode->i_sb)->si_iop_array;
220 btop = au_dbtop(dentry);
221 h_dentry = au_h_dptr(dentry, btop);
222 h_inode = d_inode(h_dentry);
223 mode = h_inode->i_mode;
224 switch (mode & S_IFMT) {
225 case S_IFREG:
226 btail = au_dbtail(dentry);
227 inode->i_op = iop + AuIop_OTHER;
228 inode->i_fop = &aufs_file_fop;
229 err = au_dy_iaop(inode, btop, h_inode);
230 if (unlikely(err))
231 goto out;
232 break;
233 case S_IFDIR:
234 isdir = 1;
235 btail = au_dbtaildir(dentry);
236 inode->i_op = iop + AuIop_DIR;
237 inode->i_fop = &aufs_dir_fop;
238 break;
239 case S_IFLNK:
240 btail = au_dbtail(dentry);
241 inode->i_op = iop + AuIop_SYMLINK;
242 break;
243 case S_IFBLK:
244 case S_IFCHR:
245 case S_IFIFO:
246 case S_IFSOCK:
247 btail = au_dbtail(dentry);
248 inode->i_op = iop + AuIop_OTHER;
249 init_special_inode(inode, mode, h_inode->i_rdev);
250 break;
251 default:
252 AuIOErr("Unknown file type 0%o\n", mode);
253 err = -EIO;
254 goto out;
255 }
256
257 /* do not set hnotify for whiteouted dirs (SHWH mode) */
258 flags = au_hi_flags(inode, isdir);
259 if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
260 && au_ftest_hi(flags, HNOTIFY)
261 && dentry->d_name.len > AUFS_WH_PFX_LEN
262 && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
263 au_fclr_hi(flags, HNOTIFY);
264 iinfo = au_ii(inode);
265 iinfo->ii_btop = btop;
266 iinfo->ii_bbot = btail;
267 for (bindex = btop; bindex <= btail; bindex++) {
268 h_dentry = au_h_dptr(dentry, bindex);
269 if (h_dentry)
270 au_set_h_iptr(inode, bindex,
271 au_igrab(d_inode(h_dentry)), flags);
272 }
273 au_cpup_attr_all(inode, /*force*/1);
274 /*
275 * to force calling aufs_get_acl() every time,
276 * do not call cache_no_acl() for aufs inode.
277 */
278
279out:
280 return err;
281}
282
283/*
284 * successful returns with iinfo write_locked
285 * minus: errno
286 * zero: success, matched
287 * plus: no error, but unmatched
288 */
289static int reval_inode(struct inode *inode, struct dentry *dentry)
290{
291 int err;
292 unsigned int gen, igflags;
293 aufs_bindex_t bindex, bbot;
294 struct inode *h_inode, *h_dinode;
295 struct dentry *h_dentry;
296
297 /*
298 * before this function, if aufs got any iinfo lock, it must be only
299 * one, the parent dir.
300 * it can happen by UDBA and the obsoleted inode number.
301 */
302 err = -EIO;
303 if (unlikely(inode->i_ino == parent_ino(dentry)))
304 goto out;
305
306 err = 1;
307 ii_write_lock_new_child(inode);
308 h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
309 h_dinode = d_inode(h_dentry);
310 bbot = au_ibbot(inode);
311 for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
312 h_inode = au_h_iptr(inode, bindex);
313 if (!h_inode || h_inode != h_dinode)
314 continue;
315
316 err = 0;
317 gen = au_iigen(inode, &igflags);
318 if (gen == au_digen(dentry)
319 && !au_ig_ftest(igflags, HALF_REFRESHED))
320 break;
321
322 /* fully refresh inode using dentry */
323 err = au_refresh_hinode(inode, dentry);
324 if (!err)
325 au_update_iigen(inode, /*half*/0);
326 break;
327 }
328
329 if (unlikely(err))
330 ii_write_unlock(inode);
331out:
332 return err;
333}
334
335int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
336 unsigned int d_type, ino_t *ino)
337{
338 int err, idx;
339 const int isnondir = d_type != DT_DIR;
340
341 /* prevent hardlinked inode number from race condition */
342 if (isnondir) {
343 err = au_xinondir_enter(sb, bindex, h_ino, &idx);
344 if (unlikely(err))
345 goto out;
346 }
347
348 err = au_xino_read(sb, bindex, h_ino, ino);
349 if (unlikely(err))
350 goto out_xinondir;
351
352 if (!*ino) {
353 err = -EIO;
354 *ino = au_xino_new_ino(sb);
355 if (unlikely(!*ino))
356 goto out_xinondir;
357 err = au_xino_write(sb, bindex, h_ino, *ino);
358 if (unlikely(err))
359 goto out_xinondir;
360 }
361
362out_xinondir:
363 if (isnondir && idx >= 0)
364 au_xinondir_leave(sb, bindex, h_ino, idx);
365out:
366 return err;
367}
368
369/* successful returns with iinfo write_locked */
370/* todo: return with unlocked? */
371struct inode *au_new_inode(struct dentry *dentry, int must_new)
372{
373 struct inode *inode, *h_inode;
374 struct dentry *h_dentry;
375 struct super_block *sb;
376 ino_t h_ino, ino;
377 int err, idx, hlinked;
378 aufs_bindex_t btop;
379
380 sb = dentry->d_sb;
381 btop = au_dbtop(dentry);
382 h_dentry = au_h_dptr(dentry, btop);
383 h_inode = d_inode(h_dentry);
384 h_ino = h_inode->i_ino;
385 hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
386
387new_ino:
388 /*
389 * stop 'race'-ing between hardlinks under different
390 * parents.
391 */
392 if (hlinked) {
393 err = au_xinondir_enter(sb, btop, h_ino, &idx);
394 inode = ERR_PTR(err);
395 if (unlikely(err))
396 goto out;
397 }
398
399 err = au_xino_read(sb, btop, h_ino, &ino);
400 inode = ERR_PTR(err);
401 if (unlikely(err))
402 goto out_xinondir;
403
404 if (!ino) {
405 ino = au_xino_new_ino(sb);
406 if (unlikely(!ino)) {
407 inode = ERR_PTR(-EIO);
408 goto out_xinondir;
409 }
410 }
411
412 AuDbg("i%lu\n", (unsigned long)ino);
413 inode = au_iget_locked(sb, ino);
414 err = PTR_ERR(inode);
415 if (IS_ERR(inode))
416 goto out_xinondir;
417
418 AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
419 if (inode->i_state & I_NEW) {
420 ii_write_lock_new_child(inode);
421 err = set_inode(inode, dentry);
422 if (!err) {
423 unlock_new_inode(inode);
424 goto out_xinondir; /* success */
425 }
426
427 /*
428 * iget_failed() calls iput(), but we need to call
429 * ii_write_unlock() after iget_failed(). so dirty hack for
430 * i_count.
431 */
432 atomic_inc(&inode->i_count);
433 iget_failed(inode);
434 ii_write_unlock(inode);
435 au_xino_write(sb, btop, h_ino, /*ino*/0);
436 /* ignore this error */
437 goto out_iput;
438 } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
439 /*
440 * horrible race condition between lookup, readdir and copyup
441 * (or something).
442 */
443 if (hlinked && idx >= 0)
444 au_xinondir_leave(sb, btop, h_ino, idx);
445 err = reval_inode(inode, dentry);
446 if (unlikely(err < 0)) {
447 hlinked = 0;
448 goto out_iput;
449 }
450 if (!err)
451 goto out; /* success */
452 else if (hlinked && idx >= 0) {
453 err = au_xinondir_enter(sb, btop, h_ino, &idx);
454 if (unlikely(err)) {
455 iput(inode);
456 inode = ERR_PTR(err);
457 goto out;
458 }
459 }
460 }
461
462 if (unlikely(au_test_fs_unique_ino(h_inode)))
463 AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
464 " b%d, %s, %pd, hi%lu, i%lu.\n",
465 btop, au_sbtype(h_dentry->d_sb), dentry,
466 (unsigned long)h_ino, (unsigned long)ino);
467 ino = 0;
468 err = au_xino_write(sb, btop, h_ino, /*ino*/0);
469 if (!err) {
470 iput(inode);
471 if (hlinked && idx >= 0)
472 au_xinondir_leave(sb, btop, h_ino, idx);
473 goto new_ino;
474 }
475
476out_iput:
477 iput(inode);
478 inode = ERR_PTR(err);
479out_xinondir:
480 if (hlinked && idx >= 0)
481 au_xinondir_leave(sb, btop, h_ino, idx);
482out:
483 return inode;
484}
485
486/* ---------------------------------------------------------------------- */
487
488int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
489 struct inode *inode)
490{
491 int err;
492 struct inode *hi;
493
494 err = au_br_rdonly(au_sbr(sb, bindex));
495
496 /* pseudo-link after flushed may happen out of bounds */
497 if (!err
498 && inode
499 && au_ibtop(inode) <= bindex
500 && bindex <= au_ibbot(inode)) {
501 /*
502 * permission check is unnecessary since vfsub routine
503 * will be called later
504 */
505 hi = au_h_iptr(inode, bindex);
506 if (hi)
507 err = IS_IMMUTABLE(hi) ? -EROFS : 0;
508 }
509
510 return err;
511}
512
513int au_test_h_perm(struct inode *h_inode, int mask)
514{
515 if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
516 return 0;
517 return inode_permission(h_inode, mask);
518}
519
520int au_test_h_perm_sio(struct inode *h_inode, int mask)
521{
522 if (au_test_nfs(h_inode->i_sb)
523 && (mask & MAY_WRITE)
524 && S_ISDIR(h_inode->i_mode))
525 mask |= MAY_READ; /* force permission check */
526 return au_test_h_perm(h_inode, mask);
527}