Recent reports of a failure to find a direct mount map entry on lookup
Subsystems (2)
Ian Kent
maintainerHi David,
Here is the patch series I currently have for this problem.
I've also tried to describe the cases I think are at play and if they are resolved by the series as well as the case I think is problematic.
Please feel free to use these in any way that is useful and any thoughts or improvements are welcome.
The little evidence I have related to this problem indicates it is related to updating (re-reading) autofs mount maps. Review of the source code surrounding this map updating shows there are a number of cases.
Indirect mount map entries are far more tolerant of map updates than direct mount maps. The reports I have are related to direct mount maps so that has been the focus of the code review.
An autofs direct mount can have multiple entries in the master map and therefore can consist of multiple maps. So the same direct map entry path can be removed from one map and added to another in the same update.
This is one case that needs to be handled and problems with this case should be fixed by the patches resulting from the code review.
Another case is where a map entry is removed but has a triggered mount covering it. Fixes for this case should also be covered buy the patches here.
The third case is where a map entry is removed from a map, there are no other instances of it, there's no real mount associated with it, but the direct mount trigger mount remains mounted following the map update. This allows the kernel to send a mount request for a map entry that doesn't exist. It results in a fatal error because automount(8) doesn't have a way to communicate the failure to the kernel as there's no ioctl file handle to do so.
This is I think the case that's been seen here and upstream and remains unresolved.
So far I have been unable to work out how a mount trigger can be left mounted when a map entry is removed. As a workaround I have improved the handling of this case in the mount request code. It now uses a method similar to the that used to reconnect to existing mounts at startup to open a file handle to send the failure status to the kernel (the last two patches). While this method should work almost all of the time it can still fail so I still need to work out why the direct mount trigger isn't being umounted on map entry removal.
Ian
Ian Kent
maintainerPATCH 1/11 autofs-5.1.9 - quiet possibly noisy log message
The message that logs when a mount is already mounted and only the timeout will be updated on map re-read can create a lot of noise in the log for direct mount maps. But this is normal operation and is of limited value for both direct and indirect maps, so remove it.
--- CHANGELOG | 1 + lib/mounts.c | 1 - 2 files changed, 1 insertion(+), 1 deletion(-)
CHANGELOG +1 -0
@@ -68,6 +68,7 @@ - fix state queue not processing state changes. - refactor mnts_get_expire_list(). - add en xtra logging entry to tree_mapent_mount_offsets(). +- quiet possibly noisy log message. 02/11/2023 autofs-5.1.9 - fix kernel mount status notification.
lib/mounts.c +0 -1
@@ -2900,7 +2900,6 @@ static int remount_active_mount(struct autofs_point *ap,/* Re-reading the map, set timeout and return */ if (ap->state ==ST_READMAP ) { -debug (ap->logopt,"already mounted, update timeout" ); ops->timeout (ap->logopt, fd,NULL , timeout); ops->close (ap->logopt, fd);return REMOUNT_READ_MAP ;
Signature · 1 line
2.51.0
Ian Kent
maintainerPATCH 2/11 autofs-5.1.9 - fix devid update on reload
In reply to Ian KentOn map update if there's a direct mount in the master map it may be associated with multiple maps.
If there's an entry that's been removed but is present in another map and the removed entry has a real mount associated with it special case handling is needed.
Currently the function that looks for these newly valid map entries doesn't do it properly. It's meant to look for the first "valid" map entry but doesn't check if the entry is valid. So if a valid entry follows the an invalid entry it isn't found, the invalid entry is returned instead.
Once this is fixed the handling of a removed direct mount entry that's covered by a real mount can be done. Basically, the newly valid map entry needs to take over the mount (which wasn't being done quite right either) and the removed map entry deleted so that the covering mount can expire. From this point onward the newly valid map entry will be used.
--- CHANGELOG | 1 + daemon/lookup.c | 38 ++++++++++++-------------------------- daemon/state.c | 17 +++++------------ include/automount.h | 2 +- 4 files changed, 19 insertions(+), 39 deletions(-)
CHANGELOG +1 -0
@@ -69,6 +69,7 @@ - refactor mnts_get_expire_list(). - add en xtra logging entry to tree_mapent_mount_offsets(). - quiet possibly noisy log message. +- fix devid update on reload. 02/11/2023 autofs-5.1.9 - fix kernel mount status notification.
daemon/lookup.c +12 -26
@@ -1348,6 +1348,7 @@ void lookup_prune_one_cache(struct autofs_point *ap, struct mapent_cache *mc, ti me =cache_enumerate (mc,NULL );while (me) { +dev_t devid;struct mapent *valid;char *key =NULL , *next_key =NULL ;
@@ -1405,6 +1406,7 @@ void lookup_prune_one_cache(struct autofs_point *ap, struct mapent_cache *mc, ti me =cache_enumerate (mc, me);continue ; } + devid = ap->type ==LKP_INDIRECT ? ap->dev : me->dev;/* * If this key has another valid entry we want to prune it,
@@ -1412,37 +1414,20 @@ void lookup_prune_one_cache(struct autofs_point *ap, struct mapent_cache *mc, ti* mount if it is a direct mount or it's just a stale indirect * cache entry. */ - valid =lookup_source_valid_mapent (ap, key,LKP_DISTINCT ); -if (valid && valid->mc == mc) { -/* -* We've found a map entry that has been removed from -* the current cache so it isn't really valid. Set the -* mapent negative to prevent further mount requests -* using the cache entry. -*/ -debug (ap->logopt,"removed map entry detected, mark negative" ); -if (valid->mapent) { -free (valid->mapent); - valid->mapent =NULL ; - } + valid =lookup_source_valid_mapent (ap, key,LKP_DISTINCT , age); +if (valid)cache_unlock (valid->mc); - valid =NULL ; - } -if (!valid && -is_mounted (path,MNTS_REAL )) { +else if (is_mounted (path,MNTS_REAL )) {debug (ap->logopt,"prune postponed, %s mounted" , path);free (key);free (path); me =cache_enumerate (mc, me);continue ; } -if (valid) -cache_unlock (valid->mc); me =cache_enumerate (mc, me);if (me) next_key =strdup (me->key); -cache_unlock (mc);cache_writelock (mc);
@@ -1455,10 +1440,7 @@ void lookup_prune_one_cache(struct autofs_point *ap, struct mapent_cache *mc, tiif (valid)cache_delete (mc, key);else if (!is_mounted (path,MNTS_AUTOFS )) { -dev_t devid = ap->dev; status =CHE_FAIL ; -if (ap->type ==LKP_DIRECT ) - devid = this->dev;if (this->ioctlfd ==-1 ) status =cache_delete (mc, key);if (status !=CHE_FAIL ) {
@@ -1532,7 +1514,7 @@ int lookup_prune_cache(struct autofs_point *ap, time_t age) }/* Return with cache readlock held */ -struct mapent *lookup_source_valid_mapent (struct autofs_point *ap,const char *key,unsigned int type) +struct mapent *lookup_source_valid_mapent (struct autofs_point *ap,const char *key,unsigned int type,time_t age) {struct master_mapent *entry = ap->entry;struct map_source *map;
@@ -1556,8 +1538,12 @@ struct mapent *lookup_source_valid_mapent(struct autofs_point *ap, const char *k me =cache_lookup_distinct (mc, key); else me =cache_lookup (mc, key); -if (me) -break ; +if (me) { +/* Valid? */ +if (me->age >= age) +break ; + me =NULL ; + }cache_unlock (mc); map = map->next; }
daemon/state.c +5 -12
@@ -344,17 +344,7 @@ static int do_readmap_mount(struct autofs_point *ap,* This is becuase of the requirement to continue running with * an empty cache awaiting a map re-load. */ - valid =lookup_source_valid_mapent (ap, me->key,LKP_DISTINCT ); -if (valid && valid->mc == me->mc) { -/* -* We've found a map entry that has been removed from -* the current cache so there is no need to update it. -* The stale entry will be dealt with when we prune the -* cache later. -*/ -cache_unlock (valid->mc); - valid =NULL ; - } + valid =lookup_source_valid_mapent (ap, me->key,LKP_DISTINCT , now);if (valid) {struct mapent_cache *vmc = valid->mc;struct ioctl_ops *ops =get_ioctl_ops ();
@@ -376,8 +366,11 @@ static int do_readmap_mount(struct autofs_point *ap,/* Take over the mount if there is one */ valid->ioctlfd = me->ioctlfd; me->ioctlfd =-1 ; +/* Same path */ + valid->dev = me->dev; + valid->ino = me->ino;/* Set device and inode number of the new mapent */ -cache_set_ino_index (vmc, me); +cache_set_ino_index (vmc, valid);cache_unlock (vmc);/* Set timeout and calculate the expire run frequency */ timeout =get_exp_timeout (ap, map);
include/automount.h +1 -1
@@ -280,7 +280,7 @@ int lookup_nss_mount(struct autofs_point *ap, struct map_source *source, const cvoid lookup_close_lookup (struct autofs_point *ap);void lookup_prune_one_cache (struct autofs_point *ap,struct mapent_cache *mc,time_t age);int lookup_prune_cache (struct autofs_point *ap,time_t age); -struct mapent *lookup_source_valid_mapent (struct autofs_point *ap,const char *key,unsigned int type); +struct mapent *lookup_source_valid_mapent (struct autofs_point *ap,const char *key,unsigned int type,time_t age);struct mapent *lookup_source_mapent (struct autofs_point *ap,const char *key,unsigned int type);int lookup_source_close_ioctlfd (struct autofs_point *ap,const char *key);
Signature · 1 line
2.51.0
Ian Kent
maintainerPATCH 3/11 autofs-5.1.9 - fix cache writelock must be taken in update_map_cache()
In reply to Ian KentIn update_map_cache() the cache writelock must be taken because we need to clean up the map entry as it will be a problem later if we don't.
It's possible that some other process has taken the lock temporarily but when this function is called the thread does not hold any cache locks so it should be ok to aquire it.
--- CHANGELOG | 1 + daemon/automount.c | 12 +++++------- 2 files changed, 6 insertions(+), 7 deletions(-)
CHANGELOG +1 -0
@@ -70,6 +70,7 @@ - add en xtra logging entry to tree_mapent_mount_offsets(). - quiet possibly noisy log message. - fix devid update on reload. +- fix cache writelock must be taken in update_map_cache(). 02/11/2023 autofs-5.1.9 - fix kernel mount status notification.
daemon/automount.c +5 -7
@@ -536,13 +536,11 @@ static void update_map_cache(struct autofs_point *ap, const char *path) } mc = map->mc; -/* If the lock is busy try later */ -if (cache_try_writelock (mc)) { - me =cache_lookup_distinct (mc, key); -if (me && me->ioctlfd ==-1 ) -cache_delete (mc, key); -cache_unlock (mc); - } +cache_writelock (mc); + me =cache_lookup_distinct (mc, key); +if (me && me->ioctlfd ==-1 ) +cache_delete (mc, key); +cache_unlock (mc); map = map->next; }
Signature · 1 line
2.51.0
Ian Kent
maintainerPATCH 4/11 autofs-5.1.9 - fix skip valid map entries on expire cleanup
In reply to Ian KentAfter an expire if the current map entry is stale because it could not be cleaned up during the map entry prune (due to the presence of a real mount) it needs to be cleaned up after a successful expire.
Currently this is done by update_map_cache() if the entry is contained in an invalid map but it should be doing it if the entry is no longer valid. In addition, if update_map_cache() gets called the umount has been successful so the ioctlfd does not need to be checked (and is in fact updated later in the expire process).
--- CHANGELOG | 1 + daemon/automount.c | 9 ++------- 2 files changed, 3 insertions(+), 7 deletions(-)
CHANGELOG +1 -0
@@ -71,6 +71,7 @@ - quiet possibly noisy log message. - fix devid update on reload. - fix cache writelock must be taken in update_map_cache(). +- fix skip valid map entries on expire cleanup. 02/11/2023 autofs-5.1.9 - fix kernel mount status notification.
daemon/automount.c +2 -7
@@ -529,16 +529,11 @@ static void update_map_cache(struct autofs_point *ap, const char *path)while (map) {struct mapent *me =NULL ; -/* Skip current, in-use cache */ -if (ap->entry->age <= map->age) { - map = map->next; -continue ; - } - mc = map->mc;cache_writelock (mc); me =cache_lookup_distinct (mc, key); -if (me && me->ioctlfd ==-1 ) +/* Only for invalid map entries */ +if (me && map->age > me->age)cache_delete (mc, key);cache_unlock (mc);
Signature · 1 line
2.51.0
Ian Kent
maintainerPATCH 5/11 autofs-5.1.9 - remove unnecessary call to set_direct_mount_tree_catatonic()
In reply to Ian KentFunction do_umount_autofs_direct() is called in two cases, during a readmap and when umounting top level direct mounts.
During a readmap, mounts should not be set catatonic so the call to set_direct_mount_tree_catatonic() is not needed. If it's called for a top level direct mount the caller, umount_autofs_direct(), calls set_direct_mount_tree_catatonic() itself already. So remove this unnecessary call.
--- CHANGELOG | 1 + daemon/direct.c | 5 +---- 2 files changed, 2 insertions(+), 4 deletions(-)
CHANGELOG +1 -0
@@ -72,6 +72,7 @@ - fix devid update on reload. - fix cache writelock must be taken in update_map_cache(). - fix skip valid map entries on expire cleanup. +- remove unnecessary call to set_direct_mount_tree_catatonic(). 02/11/2023 autofs-5.1.9 - fix kernel mount status notification.
daemon/direct.c +1 -4
@@ -186,11 +186,8 @@ int do_umount_autofs_direct(struct autofs_point *ap, struct mapent *me)warn (ap->logopt,"mount point %s is in use" , me->key);if (ap->state ==ST_SHUTDOWN_FORCE ) goto force_umount; -else { -if (ap->state !=ST_READMAP ) -set_direct_mount_tree_catatonic (ap, me); +else return 0 ; - }break ;case ENOTDIR :error (ap->logopt,"mount point is not a directory" );
Signature · 1 line
2.51.0
Ian Kent
maintainerPATCH 6/11 autofs-5.1.9 - remove unnecessary assignment in umount_multi()
In reply to Ian KentRemove the leftover initialisation of left from when there were multiple calls to umount_subtree_mounts().
--- CHANGELOG | 1 + daemon/automount.c | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-)
CHANGELOG +1 -0
@@ -73,6 +73,7 @@ - fix cache writelock must be taken in update_map_cache(). - fix skip valid map entries on expire cleanup. - remove unnecessary call to set_direct_mount_tree_catatonic(). +- remove unnecessary assignment in umount_multi(). 02/11/2023 autofs-5.1.9 - fix kernel mount status notification.
daemon/automount.c +1 -3
@@ -719,9 +719,7 @@ int umount_multi(struct autofs_point *ap, const char *path, int incl)mnts_put_mount (sbmnt); } - left =0 ; - - left +=umount_subtree_mounts (ap, path, is_autofs_fs); + left =umount_subtree_mounts (ap, path, is_autofs_fs);/* Delete detritus like unwanted mountpoints and symlinks */ if (left == 0 &&
Signature · 1 line
2.51.0
Ian Kent
maintainerPATCH 7/11 autofs-5.1.9 - fix direct mount trigger umount failure case
In reply to Ian KentIn function do_umount_autofs_direct() for the case where the trigger mount is found to be in use we should be detaching the mount so it gets umounted but the process can continue using it while it has an open file handle for it.
This is because direct mount triggers are only umounted when the map entry is removed from a map or automount(8) is shutdown which in both cases the trigger mount needs to go away.
--- CHANGELOG | 1 + daemon/direct.c | 1 + 2 files changed, 2 insertions(+)
CHANGELOG +1 -0
@@ -74,6 +74,7 @@ - fix skip valid map entries on expire cleanup. - remove unnecessary call to set_direct_mount_tree_catatonic(). - remove unnecessary assignment in umount_multi(). +- fix direct mount trigger umount failure case. 02/11/2023 autofs-5.1.9 - fix kernel mount status notification.
daemon/direct.c +1 -0
@@ -153,6 +153,7 @@ int do_umount_autofs_direct(struct autofs_point *ap, struct mapent *me) }else { me->ioctlfd =-1 ; ops->close (ap->logopt, ioctlfd); + rv =-1 ; goto force_umount; } }
Signature · 1 line
2.51.0
Ian Kent
maintainerPATCH 8/11 autofs-5.1.9 - refactor do_umount_autofs_direct()
In reply to Ian KentRefactor functon do_umount_autofs_direct() so that it can be called from do_expire_direct() to clean up stale direct mounts that couldn't be cleaned up at map re-load.
--- CHANGELOG | 1 + daemon/direct.c | 106 ++++++++++++++++++++++++++---------------------- 2 files changed, 58 insertions(+), 49 deletions(-)
CHANGELOG +1 -0
@@ -75,6 +75,7 @@ - remove unnecessary call to set_direct_mount_tree_catatonic(). - remove unnecessary assignment in umount_multi(). - fix direct mount trigger umount failure case. +- refactor do_umount_autofs_direct(). 02/11/2023 autofs-5.1.9 - fix kernel mount status notification.
daemon/direct.c +57 -49
129 lines not on this page · raw message
Signature · 1 line
2.51.0
Ian Kent
maintainerPATCH 9/11 autofs-5.1.9 - fix stale direct mount trigger not umounted on expire
In reply to Ian KentIf a direct mount map entry is removed but has an active real mount the mount trigger needs to be unmounted during the expire cleanup.
If the direct mount map entry has been re-added the map entry age will have been updated so the entry won't be seen as stale so the umount won't be done.
Also in function umount_multi() update_map_cache() and check_rm_dirs() are not called for direct mounts because count_mounts() always returns 1 or more for top level direct mounts. Make this clear by using ap->type in the logical check and rely on the left == 0 check to verify there are no remaining mounts for indirect mounts since count_mounts() will be more expensive.
--- CHANGELOG | 1 + daemon/automount.c | 12 ++++++++---- daemon/direct.c | 22 +++++++++++++++++++++- 3 files changed, 30 insertions(+), 5 deletions(-)
CHANGELOG +1 -0
@@ -76,6 +76,7 @@ - remove unnecessary assignment in umount_multi(). - fix direct mount trigger umount failure case. - refactor do_umount_autofs_direct(). +- fix stale direct mount trigger not umounted on expire. 02/11/2023 autofs-5.1.9 - fix kernel mount status notification.
daemon/automount.c +8 -4
@@ -721,10 +721,14 @@ int umount_multi(struct autofs_point *ap, const char *path, int incl) left =umount_subtree_mounts (ap, path, is_autofs_fs); -/* Delete detritus like unwanted mountpoints and symlinks */ -if (left ==0 && - ap->state !=ST_READMAP && - !count_mounts (ap, path, ap->dev)) { +/* Delete detritus like unwanted mountpoints and symlinks +* for indirect mounts. This can't be done for direct mounts +* here because there's an ioctl file handle open on the +* autofs trigger mount for them so it must be done after +* the expire. +*/ +if (ap->type ==LKP_INDIRECT && + ap->state !=ST_READMAP && left ==0 ) {update_map_cache (ap, path);check_rm_dirs (ap, path, incl); }
daemon/direct.c +21 -1
@@ -1005,10 +1005,30 @@ static void *do_expire_direct(void *arg) mt.ioctlfd, mt.wait_queue_token, -ENOENT); else {struct mapent *me; +cache_writelock (mt.mc); me =cache_lookup_distinct (mt.mc, mt.name); -if (me) +if (me) { +/* If the direct mount map entry is no longer +* valid but there is an autofs mount trigger +* for the mount the mount trigger needs to be +* umounted, the map entry deleted and the mount +* point directory removed (if it was created by +* us). +*/ me->ioctlfd =-1 ; +if (me->mc->map->age > me->age && +is_mounted (mt.name,MNTS_AUTOFS )) { +/* We must detach the mount becuase the +* umount must be completed before +* notifying status to the kernel but +* there's an ioctlfd open on the +* trigger. +*/ +if (!finish_umount (ap, me,-1 )) +cache_delete (me->mc, me->key); + } + }cache_unlock (mt.mc); ops->send_ready (ap->logopt, mt.ioctlfd, mt.wait_queue_token); ops->close (ap->logopt, mt.ioctlfd);
Signature · 1 line
2.51.0
Ian Kent
maintainerPATCH 10/11 autofs-5.1.9 - add function table_lookup_ino()
In reply to Ian KentAdd function table_lookup_ino() to try and locate a mount for a given device, open a file handle for it, and return it's path in the provided buffer.
--- CHANGELOG | 1 + include/mounts.h | 1 + lib/mounts.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+)
CHANGELOG +1 -0
@@ -77,6 +77,7 @@ - fix direct mount trigger umount failure case. - refactor do_umount_autofs_direct(). - fix stale direct mount trigger not umounted on expire. +- add function table_lookup_ino(). 02/11/2023 autofs-5.1.9 - fix kernel mount status notification.
include/mounts.h +1 -0
@@ -175,6 +175,7 @@ void mnts_remove_amdmounts(struct autofs_point *ap);struct mnt_list *mnts_add_mount (struct autofs_point *ap,const char *name,unsigned int flags);void mnts_remove_mount (const char *mp,unsigned int flags);struct mnt_list *get_mnt_list (const char *path,int include); +char *table_lookup_ino (struct autofs_point *ap,dev_t dev,ino_t ino,char *buf,size_t len,int *fd);unsigned int mnts_has_mounted_mounts (struct autofs_point *ap);int tree_traverse_inorder (struct tree_node *n,tree_work_fn_t work,void *ptr);void tree_free (struct tree_node *root);
lib/mounts.c +70 -0
76 lines not on this page · raw message
Signature · 1 line
2.51.0
Ian Kent
maintainerPATCH 11/11 autofs-5.1.9 - improve handling of missing map entry for mount request
In reply to Ian KentIf the map entry isn't found it must have been deleted from the map but a trigger mount is still mounted because it has sent us this request. Use the mount table for a brute force lookup to get the path and open a file handle for it so we can send a failure status to the kernel.
Also remove the crit() log message following open_ioctlfd() as it already issues an appropriate error message on failure.
--- CHANGELOG | 1 + daemon/direct.c | 49 +++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 6 deletions(-)
CHANGELOG +1 -0
@@ -78,6 +78,7 @@ - refactor do_umount_autofs_direct(). - fix stale direct mount trigger not umounted on expire. - add function table_lookup_ino(). +- improve handling of missing map entry for mount request. 02/11/2023 autofs-5.1.9 - fix kernel mount status notification.
daemon/direct.c +43 -6
62 lines not on this page · raw message
Signature · 1 line
2.51.0
Hi Ian,
Thanks a lot for sending through this patch set! I've been mostly focused on the last problem you describe with stale triggers following map removal, which appeared to regress via autofs commit 21ce28d.
I'll have a closer look at the changes and let you know how they fare through my test env reproducer.
Cheers, David