0001
0002
0003
0004
0005
0006
0007
0008 #include "autofs_i.h"
0009
0010
0011 static inline int autofs_can_expire(struct dentry *dentry,
0012 unsigned long timeout, unsigned int how)
0013 {
0014 struct autofs_info *ino = autofs_dentry_ino(dentry);
0015
0016
0017 if (ino == NULL)
0018 return 0;
0019
0020 if (!(how & AUTOFS_EXP_IMMEDIATE)) {
0021
0022 if (!timeout || time_after(ino->last_used + timeout, jiffies))
0023 return 0;
0024 }
0025 return 1;
0026 }
0027
0028
0029 static int autofs_mount_busy(struct vfsmount *mnt,
0030 struct dentry *dentry, unsigned int how)
0031 {
0032 struct dentry *top = dentry;
0033 struct path path = {.mnt = mnt, .dentry = dentry};
0034 int status = 1;
0035
0036 pr_debug("dentry %p %pd\n", dentry, dentry);
0037
0038 path_get(&path);
0039
0040 if (!follow_down_one(&path))
0041 goto done;
0042
0043 if (is_autofs_dentry(path.dentry)) {
0044 struct autofs_sb_info *sbi = autofs_sbi(path.dentry->d_sb);
0045
0046
0047 if (autofs_type_indirect(sbi->type))
0048 goto done;
0049 }
0050
0051
0052 if (how & AUTOFS_EXP_FORCED) {
0053 status = 0;
0054 goto done;
0055 }
0056
0057
0058 if (!may_umount_tree(path.mnt)) {
0059 struct autofs_info *ino;
0060
0061 ino = autofs_dentry_ino(top);
0062 ino->last_used = jiffies;
0063 goto done;
0064 }
0065
0066 status = 0;
0067 done:
0068 pr_debug("returning = %d\n", status);
0069 path_put(&path);
0070 return status;
0071 }
0072
0073
0074 static struct dentry *positive_after(struct dentry *p, struct dentry *child)
0075 {
0076 if (child)
0077 child = list_next_entry(child, d_child);
0078 else
0079 child = list_first_entry(&p->d_subdirs, struct dentry, d_child);
0080
0081 list_for_each_entry_from(child, &p->d_subdirs, d_child) {
0082 spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
0083 if (simple_positive(child)) {
0084 dget_dlock(child);
0085 spin_unlock(&child->d_lock);
0086 return child;
0087 }
0088 spin_unlock(&child->d_lock);
0089 }
0090
0091 return NULL;
0092 }
0093
0094
0095
0096
0097 static struct dentry *get_next_positive_subdir(struct dentry *prev,
0098 struct dentry *root)
0099 {
0100 struct autofs_sb_info *sbi = autofs_sbi(root->d_sb);
0101 struct dentry *q;
0102
0103 spin_lock(&sbi->lookup_lock);
0104 spin_lock(&root->d_lock);
0105 q = positive_after(root, prev);
0106 spin_unlock(&root->d_lock);
0107 spin_unlock(&sbi->lookup_lock);
0108 dput(prev);
0109 return q;
0110 }
0111
0112
0113
0114
0115 static struct dentry *get_next_positive_dentry(struct dentry *prev,
0116 struct dentry *root)
0117 {
0118 struct autofs_sb_info *sbi = autofs_sbi(root->d_sb);
0119 struct dentry *p = prev, *ret = NULL, *d = NULL;
0120
0121 if (prev == NULL)
0122 return dget(root);
0123
0124 spin_lock(&sbi->lookup_lock);
0125 spin_lock(&p->d_lock);
0126 while (1) {
0127 struct dentry *parent;
0128
0129 ret = positive_after(p, d);
0130 if (ret || p == root)
0131 break;
0132 parent = p->d_parent;
0133 spin_unlock(&p->d_lock);
0134 spin_lock(&parent->d_lock);
0135 d = p;
0136 p = parent;
0137 }
0138 spin_unlock(&p->d_lock);
0139 spin_unlock(&sbi->lookup_lock);
0140 dput(prev);
0141 return ret;
0142 }
0143
0144
0145
0146
0147
0148
0149
0150 static int autofs_direct_busy(struct vfsmount *mnt,
0151 struct dentry *top,
0152 unsigned long timeout,
0153 unsigned int how)
0154 {
0155 pr_debug("top %p %pd\n", top, top);
0156
0157
0158 if (how & AUTOFS_EXP_FORCED)
0159 return 0;
0160
0161
0162 if (!may_umount_tree(mnt)) {
0163 struct autofs_info *ino;
0164
0165 ino = autofs_dentry_ino(top);
0166 if (ino)
0167 ino->last_used = jiffies;
0168 return 1;
0169 }
0170
0171
0172 if (!autofs_can_expire(top, timeout, how))
0173 return 1;
0174
0175 return 0;
0176 }
0177
0178
0179
0180
0181
0182 static int autofs_tree_busy(struct vfsmount *mnt,
0183 struct dentry *top,
0184 unsigned long timeout,
0185 unsigned int how)
0186 {
0187 struct autofs_info *top_ino = autofs_dentry_ino(top);
0188 struct dentry *p;
0189
0190 pr_debug("top %p %pd\n", top, top);
0191
0192
0193 if (!simple_positive(top))
0194 return 1;
0195
0196 p = NULL;
0197 while ((p = get_next_positive_dentry(p, top))) {
0198 pr_debug("dentry %p %pd\n", p, p);
0199
0200
0201
0202
0203
0204
0205
0206 if (d_mountpoint(p)) {
0207 if (autofs_mount_busy(mnt, p, how)) {
0208 top_ino->last_used = jiffies;
0209 dput(p);
0210 return 1;
0211 }
0212 } else {
0213 struct autofs_info *ino = autofs_dentry_ino(p);
0214 unsigned int ino_count = READ_ONCE(ino->count);
0215
0216
0217 if (p == top)
0218 ino_count += 2;
0219 else
0220 ino_count++;
0221
0222 if (d_count(p) > ino_count) {
0223 top_ino->last_used = jiffies;
0224 dput(p);
0225 return 1;
0226 }
0227 }
0228 }
0229
0230
0231 if (how & AUTOFS_EXP_FORCED)
0232 return 0;
0233
0234
0235 if (!autofs_can_expire(top, timeout, how))
0236 return 1;
0237
0238 return 0;
0239 }
0240
0241 static struct dentry *autofs_check_leaves(struct vfsmount *mnt,
0242 struct dentry *parent,
0243 unsigned long timeout,
0244 unsigned int how)
0245 {
0246 struct dentry *p;
0247
0248 pr_debug("parent %p %pd\n", parent, parent);
0249
0250 p = NULL;
0251 while ((p = get_next_positive_dentry(p, parent))) {
0252 pr_debug("dentry %p %pd\n", p, p);
0253
0254 if (d_mountpoint(p)) {
0255
0256 if (autofs_mount_busy(mnt, p, how))
0257 continue;
0258
0259
0260
0261
0262 if (how & AUTOFS_EXP_FORCED)
0263 return p;
0264
0265
0266 if (autofs_can_expire(p, timeout, how))
0267 return p;
0268 }
0269 }
0270 return NULL;
0271 }
0272
0273
0274 static struct dentry *autofs_expire_direct(struct super_block *sb,
0275 struct vfsmount *mnt,
0276 struct autofs_sb_info *sbi,
0277 unsigned int how)
0278 {
0279 struct dentry *root = dget(sb->s_root);
0280 struct autofs_info *ino;
0281 unsigned long timeout;
0282
0283 if (!root)
0284 return NULL;
0285
0286 timeout = sbi->exp_timeout;
0287
0288 if (!autofs_direct_busy(mnt, root, timeout, how)) {
0289 spin_lock(&sbi->fs_lock);
0290 ino = autofs_dentry_ino(root);
0291
0292 if (ino->flags & AUTOFS_INF_PENDING) {
0293 spin_unlock(&sbi->fs_lock);
0294 goto out;
0295 }
0296 ino->flags |= AUTOFS_INF_WANT_EXPIRE;
0297 spin_unlock(&sbi->fs_lock);
0298 synchronize_rcu();
0299 if (!autofs_direct_busy(mnt, root, timeout, how)) {
0300 spin_lock(&sbi->fs_lock);
0301 ino->flags |= AUTOFS_INF_EXPIRING;
0302 init_completion(&ino->expire_complete);
0303 spin_unlock(&sbi->fs_lock);
0304 return root;
0305 }
0306 spin_lock(&sbi->fs_lock);
0307 ino->flags &= ~AUTOFS_INF_WANT_EXPIRE;
0308 spin_unlock(&sbi->fs_lock);
0309 }
0310 out:
0311 dput(root);
0312
0313 return NULL;
0314 }
0315
0316
0317
0318
0319
0320
0321 static struct dentry *should_expire(struct dentry *dentry,
0322 struct vfsmount *mnt,
0323 unsigned long timeout,
0324 unsigned int how)
0325 {
0326 struct autofs_info *ino = autofs_dentry_ino(dentry);
0327 unsigned int ino_count;
0328
0329
0330 if (ino->flags & AUTOFS_INF_PENDING)
0331 return NULL;
0332
0333
0334
0335
0336
0337
0338
0339 if (d_mountpoint(dentry)) {
0340 pr_debug("checking mountpoint %p %pd\n", dentry, dentry);
0341
0342
0343 if (autofs_mount_busy(mnt, dentry, how))
0344 return NULL;
0345
0346
0347
0348
0349 if (how & AUTOFS_EXP_FORCED)
0350 return dentry;
0351
0352
0353 if (autofs_can_expire(dentry, timeout, how))
0354 return dentry;
0355 return NULL;
0356 }
0357
0358 if (d_is_symlink(dentry)) {
0359 pr_debug("checking symlink %p %pd\n", dentry, dentry);
0360
0361
0362 if (how & AUTOFS_EXP_FORCED)
0363 return dentry;
0364
0365
0366
0367
0368
0369 if (autofs_can_expire(dentry, timeout, how))
0370 return dentry;
0371 return NULL;
0372 }
0373
0374 if (autofs_empty(ino))
0375 return NULL;
0376
0377
0378 if (!(how & AUTOFS_EXP_LEAVES)) {
0379
0380 if (!(how & AUTOFS_EXP_FORCED)) {
0381
0382 ino_count = READ_ONCE(ino->count) + 1;
0383 if (d_count(dentry) > ino_count)
0384 return NULL;
0385 }
0386
0387 if (!autofs_tree_busy(mnt, dentry, timeout, how))
0388 return dentry;
0389
0390
0391
0392
0393 } else {
0394 struct dentry *expired;
0395
0396
0397 if (!(how & AUTOFS_EXP_FORCED)) {
0398
0399 ino_count = READ_ONCE(ino->count) + 1;
0400 if (d_count(dentry) > ino_count)
0401 return NULL;
0402 }
0403
0404 expired = autofs_check_leaves(mnt, dentry, timeout, how);
0405 if (expired) {
0406 if (expired == dentry)
0407 dput(dentry);
0408 return expired;
0409 }
0410 }
0411 return NULL;
0412 }
0413
0414
0415
0416
0417
0418
0419
0420 static struct dentry *autofs_expire_indirect(struct super_block *sb,
0421 struct vfsmount *mnt,
0422 struct autofs_sb_info *sbi,
0423 unsigned int how)
0424 {
0425 unsigned long timeout;
0426 struct dentry *root = sb->s_root;
0427 struct dentry *dentry;
0428 struct dentry *expired;
0429 struct dentry *found;
0430 struct autofs_info *ino;
0431
0432 if (!root)
0433 return NULL;
0434
0435 timeout = sbi->exp_timeout;
0436
0437 dentry = NULL;
0438 while ((dentry = get_next_positive_subdir(dentry, root))) {
0439 spin_lock(&sbi->fs_lock);
0440 ino = autofs_dentry_ino(dentry);
0441 if (ino->flags & AUTOFS_INF_WANT_EXPIRE) {
0442 spin_unlock(&sbi->fs_lock);
0443 continue;
0444 }
0445 spin_unlock(&sbi->fs_lock);
0446
0447 expired = should_expire(dentry, mnt, timeout, how);
0448 if (!expired)
0449 continue;
0450
0451 spin_lock(&sbi->fs_lock);
0452 ino = autofs_dentry_ino(expired);
0453 ino->flags |= AUTOFS_INF_WANT_EXPIRE;
0454 spin_unlock(&sbi->fs_lock);
0455 synchronize_rcu();
0456
0457
0458
0459
0460 how &= ~AUTOFS_EXP_LEAVES;
0461 found = should_expire(expired, mnt, timeout, how);
0462 if (found != expired) {
0463 dput(found);
0464 goto next;
0465 }
0466
0467 if (expired != dentry)
0468 dput(dentry);
0469
0470 spin_lock(&sbi->fs_lock);
0471 goto found;
0472 next:
0473 spin_lock(&sbi->fs_lock);
0474 ino->flags &= ~AUTOFS_INF_WANT_EXPIRE;
0475 spin_unlock(&sbi->fs_lock);
0476 if (expired != dentry)
0477 dput(expired);
0478 }
0479 return NULL;
0480
0481 found:
0482 pr_debug("returning %p %pd\n", expired, expired);
0483 ino->flags |= AUTOFS_INF_EXPIRING;
0484 init_completion(&ino->expire_complete);
0485 spin_unlock(&sbi->fs_lock);
0486 return expired;
0487 }
0488
0489 int autofs_expire_wait(const struct path *path, int rcu_walk)
0490 {
0491 struct dentry *dentry = path->dentry;
0492 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
0493 struct autofs_info *ino = autofs_dentry_ino(dentry);
0494 int status;
0495 int state;
0496
0497
0498 if (!(ino->flags & AUTOFS_INF_WANT_EXPIRE))
0499 return 0;
0500 if (rcu_walk)
0501 return -ECHILD;
0502
0503 retry:
0504 spin_lock(&sbi->fs_lock);
0505 state = ino->flags & (AUTOFS_INF_WANT_EXPIRE | AUTOFS_INF_EXPIRING);
0506 if (state == AUTOFS_INF_WANT_EXPIRE) {
0507 spin_unlock(&sbi->fs_lock);
0508
0509
0510
0511
0512 schedule_timeout_uninterruptible(HZ/10);
0513 goto retry;
0514 }
0515 if (state & AUTOFS_INF_EXPIRING) {
0516 spin_unlock(&sbi->fs_lock);
0517
0518 pr_debug("waiting for expire %p name=%pd\n", dentry, dentry);
0519
0520 status = autofs_wait(sbi, path, NFY_NONE);
0521 wait_for_completion(&ino->expire_complete);
0522
0523 pr_debug("expire done status=%d\n", status);
0524
0525 if (d_unhashed(dentry))
0526 return -EAGAIN;
0527
0528 return status;
0529 }
0530 spin_unlock(&sbi->fs_lock);
0531
0532 return 0;
0533 }
0534
0535
0536 int autofs_expire_run(struct super_block *sb,
0537 struct vfsmount *mnt,
0538 struct autofs_sb_info *sbi,
0539 struct autofs_packet_expire __user *pkt_p)
0540 {
0541 struct autofs_packet_expire pkt;
0542 struct autofs_info *ino;
0543 struct dentry *dentry;
0544 int ret = 0;
0545
0546 memset(&pkt, 0, sizeof(pkt));
0547
0548 pkt.hdr.proto_version = sbi->version;
0549 pkt.hdr.type = autofs_ptype_expire;
0550
0551 dentry = autofs_expire_indirect(sb, mnt, sbi, 0);
0552 if (!dentry)
0553 return -EAGAIN;
0554
0555 pkt.len = dentry->d_name.len;
0556 memcpy(pkt.name, dentry->d_name.name, pkt.len);
0557 pkt.name[pkt.len] = '\0';
0558
0559 if (copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)))
0560 ret = -EFAULT;
0561
0562 spin_lock(&sbi->fs_lock);
0563 ino = autofs_dentry_ino(dentry);
0564
0565 ino->last_used = jiffies;
0566 ino->flags &= ~(AUTOFS_INF_EXPIRING|AUTOFS_INF_WANT_EXPIRE);
0567 complete_all(&ino->expire_complete);
0568 spin_unlock(&sbi->fs_lock);
0569
0570 dput(dentry);
0571
0572 return ret;
0573 }
0574
0575 int autofs_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
0576 struct autofs_sb_info *sbi, unsigned int how)
0577 {
0578 struct dentry *dentry;
0579 int ret = -EAGAIN;
0580
0581 if (autofs_type_trigger(sbi->type))
0582 dentry = autofs_expire_direct(sb, mnt, sbi, how);
0583 else
0584 dentry = autofs_expire_indirect(sb, mnt, sbi, how);
0585
0586 if (dentry) {
0587 struct autofs_info *ino = autofs_dentry_ino(dentry);
0588 const struct path path = { .mnt = mnt, .dentry = dentry };
0589
0590
0591
0592
0593 ret = autofs_wait(sbi, &path, NFY_EXPIRE);
0594
0595 spin_lock(&sbi->fs_lock);
0596
0597 ino->last_used = jiffies;
0598 ino->flags &= ~(AUTOFS_INF_EXPIRING|AUTOFS_INF_WANT_EXPIRE);
0599 complete_all(&ino->expire_complete);
0600 spin_unlock(&sbi->fs_lock);
0601 dput(dentry);
0602 }
0603
0604 return ret;
0605 }
0606
0607
0608
0609
0610
0611 int autofs_expire_multi(struct super_block *sb, struct vfsmount *mnt,
0612 struct autofs_sb_info *sbi, int __user *arg)
0613 {
0614 unsigned int how = 0;
0615
0616 if (arg && get_user(how, arg))
0617 return -EFAULT;
0618
0619 return autofs_do_expire_multi(sb, mnt, sbi, how);
0620 }