xref: /OK3568_Linux_fs/kernel/Documentation/filesystems/autofs.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun=====================
2*4882a593Smuzhiyunautofs - how it works
3*4882a593Smuzhiyun=====================
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunPurpose
6*4882a593Smuzhiyun=======
7*4882a593Smuzhiyun
8*4882a593SmuzhiyunThe goal of autofs is to provide on-demand mounting and race free
9*4882a593Smuzhiyunautomatic unmounting of various other filesystems.  This provides two
10*4882a593Smuzhiyunkey advantages:
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun1. There is no need to delay boot until all filesystems that
13*4882a593Smuzhiyun   might be needed are mounted.  Processes that try to access those
14*4882a593Smuzhiyun   slow filesystems might be delayed but other processes can
15*4882a593Smuzhiyun   continue freely.  This is particularly important for
16*4882a593Smuzhiyun   network filesystems (e.g. NFS) or filesystems stored on
17*4882a593Smuzhiyun   media with a media-changing robot.
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun2. The names and locations of filesystems can be stored in
20*4882a593Smuzhiyun   a remote database and can change at any time.  The content
21*4882a593Smuzhiyun   in that data base at the time of access will be used to provide
22*4882a593Smuzhiyun   a target for the access.  The interpretation of names in the
23*4882a593Smuzhiyun   filesystem can even be programmatic rather than database-backed,
24*4882a593Smuzhiyun   allowing wildcards for example, and can vary based on the user who
25*4882a593Smuzhiyun   first accessed a name.
26*4882a593Smuzhiyun
27*4882a593SmuzhiyunContext
28*4882a593Smuzhiyun=======
29*4882a593Smuzhiyun
30*4882a593SmuzhiyunThe "autofs" filesystem module is only one part of an autofs system.
31*4882a593SmuzhiyunThere also needs to be a user-space program which looks up names
32*4882a593Smuzhiyunand mounts filesystems.  This will often be the "automount" program,
33*4882a593Smuzhiyunthough other tools including "systemd" can make use of "autofs".
34*4882a593SmuzhiyunThis document describes only the kernel module and the interactions
35*4882a593Smuzhiyunrequired with any user-space program.  Subsequent text refers to this
36*4882a593Smuzhiyunas the "automount daemon" or simply "the daemon".
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun"autofs" is a Linux kernel module with provides the "autofs"
39*4882a593Smuzhiyunfilesystem type.  Several "autofs" filesystems can be mounted and they
40*4882a593Smuzhiyuncan each be managed separately, or all managed by the same daemon.
41*4882a593Smuzhiyun
42*4882a593SmuzhiyunContent
43*4882a593Smuzhiyun=======
44*4882a593Smuzhiyun
45*4882a593SmuzhiyunAn autofs filesystem can contain 3 sorts of objects: directories,
46*4882a593Smuzhiyunsymbolic links and mount traps.  Mount traps are directories with
47*4882a593Smuzhiyunextra properties as described in the next section.
48*4882a593Smuzhiyun
49*4882a593SmuzhiyunObjects can only be created by the automount daemon: symlinks are
50*4882a593Smuzhiyuncreated with a regular `symlink` system call, while directories and
51*4882a593Smuzhiyunmount traps are created with `mkdir`.  The determination of whether a
52*4882a593Smuzhiyundirectory should be a mount trap is based on a master map. This master
53*4882a593Smuzhiyunmap is consulted by autofs to determine which directories are mount
54*4882a593Smuzhiyunpoints. Mount points can be *direct*/*indirect*/*offset*.
55*4882a593SmuzhiyunOn most systems, the default master map is located at */etc/auto.master*.
56*4882a593Smuzhiyun
57*4882a593SmuzhiyunIf neither the *direct* or *offset* mount options are given (so the
58*4882a593Smuzhiyunmount is considered to be *indirect*), then the root directory is
59*4882a593Smuzhiyunalways a regular directory, otherwise it is a mount trap when it is
60*4882a593Smuzhiyunempty and a regular directory when not empty.  Note that *direct* and
61*4882a593Smuzhiyun*offset* are treated identically so a concise summary is that the root
62*4882a593Smuzhiyundirectory is a mount trap only if the filesystem is mounted *direct*
63*4882a593Smuzhiyunand the root is empty.
64*4882a593Smuzhiyun
65*4882a593SmuzhiyunDirectories created in the root directory are mount traps only if the
66*4882a593Smuzhiyunfilesystem is mounted *indirect* and they are empty.
67*4882a593Smuzhiyun
68*4882a593SmuzhiyunDirectories further down the tree depend on the *maxproto* mount
69*4882a593Smuzhiyunoption and particularly whether it is less than five or not.
70*4882a593SmuzhiyunWhen *maxproto* is five, no directories further down the
71*4882a593Smuzhiyuntree are ever mount traps, they are always regular directories.  When
72*4882a593Smuzhiyunthe *maxproto* is four (or three), these directories are mount traps
73*4882a593Smuzhiyunprecisely when they are empty.
74*4882a593Smuzhiyun
75*4882a593SmuzhiyunSo: non-empty (i.e. non-leaf) directories are never mount traps. Empty
76*4882a593Smuzhiyundirectories are sometimes mount traps, and sometimes not depending on
77*4882a593Smuzhiyunwhere in the tree they are (root, top level, or lower), the *maxproto*,
78*4882a593Smuzhiyunand whether the mount was *indirect* or not.
79*4882a593Smuzhiyun
80*4882a593SmuzhiyunMount Traps
81*4882a593Smuzhiyun===========
82*4882a593Smuzhiyun
83*4882a593SmuzhiyunA core element of the implementation of autofs is the Mount Traps
84*4882a593Smuzhiyunwhich are provided by the Linux VFS.  Any directory provided by a
85*4882a593Smuzhiyunfilesystem can be designated as a trap.  This involves two separate
86*4882a593Smuzhiyunfeatures that work together to allow autofs to do its job.
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun**DCACHE_NEED_AUTOMOUNT**
89*4882a593Smuzhiyun
90*4882a593SmuzhiyunIf a dentry has the DCACHE_NEED_AUTOMOUNT flag set (which gets set if
91*4882a593Smuzhiyunthe inode has S_AUTOMOUNT set, or can be set directly) then it is
92*4882a593Smuzhiyun(potentially) a mount trap.  Any access to this directory beyond a
93*4882a593Smuzhiyun"`stat`" will (normally) cause the `d_op->d_automount()` dentry operation
94*4882a593Smuzhiyunto be called. The task of this method is to find the filesystem that
95*4882a593Smuzhiyunshould be mounted on the directory and to return it.  The VFS is
96*4882a593Smuzhiyunresponsible for actually mounting the root of this filesystem on the
97*4882a593Smuzhiyundirectory.
98*4882a593Smuzhiyun
99*4882a593Smuzhiyunautofs doesn't find the filesystem itself but sends a message to the
100*4882a593Smuzhiyunautomount daemon asking it to find and mount the filesystem.  The
101*4882a593Smuzhiyunautofs `d_automount` method then waits for the daemon to report that
102*4882a593Smuzhiyuneverything is ready.  It will then return "`NULL`" indicating that the
103*4882a593Smuzhiyunmount has already happened.  The VFS doesn't try to mount anything but
104*4882a593Smuzhiyunfollows down the mount that is already there.
105*4882a593Smuzhiyun
106*4882a593SmuzhiyunThis functionality is sufficient for some users of mount traps such
107*4882a593Smuzhiyunas NFS which creates traps so that mountpoints on the server can be
108*4882a593Smuzhiyunreflected on the client.  However it is not sufficient for autofs.  As
109*4882a593Smuzhiyunmounting onto a directory is considered to be "beyond a `stat`", the
110*4882a593Smuzhiyunautomount daemon would not be able to mount a filesystem on the 'trap'
111*4882a593Smuzhiyundirectory without some way to avoid getting caught in the trap.  For
112*4882a593Smuzhiyunthat purpose there is another flag.
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun**DCACHE_MANAGE_TRANSIT**
115*4882a593Smuzhiyun
116*4882a593SmuzhiyunIf a dentry has DCACHE_MANAGE_TRANSIT set then two very different but
117*4882a593Smuzhiyunrelated behaviours are invoked, both using the `d_op->d_manage()`
118*4882a593Smuzhiyundentry operation.
119*4882a593Smuzhiyun
120*4882a593SmuzhiyunFirstly, before checking to see if any filesystem is mounted on the
121*4882a593Smuzhiyundirectory, d_manage() will be called with the `rcu_walk` parameter set
122*4882a593Smuzhiyunto `false`.  It may return one of three things:
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun-  A return value of zero indicates that there is nothing special
125*4882a593Smuzhiyun   about this dentry and normal checks for mounts and automounts
126*4882a593Smuzhiyun   should proceed.
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun   autofs normally returns zero, but first waits for any
129*4882a593Smuzhiyun   expiry (automatic unmounting of the mounted filesystem) to
130*4882a593Smuzhiyun   complete.  This avoids races.
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun-  A return value of `-EISDIR` tells the VFS to ignore any mounts
133*4882a593Smuzhiyun   on the directory and to not consider calling `->d_automount()`.
134*4882a593Smuzhiyun   This effectively disables the **DCACHE_NEED_AUTOMOUNT** flag
135*4882a593Smuzhiyun   causing the directory not be a mount trap after all.
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun   autofs returns this if it detects that the process performing the
138*4882a593Smuzhiyun   lookup is the automount daemon and that the mount has been
139*4882a593Smuzhiyun   requested but has not yet completed.  How it determines this is
140*4882a593Smuzhiyun   discussed later.  This allows the automount daemon not to get
141*4882a593Smuzhiyun   caught in the mount trap.
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun   There is a subtlety here.  It is possible that a second autofs
144*4882a593Smuzhiyun   filesystem can be mounted below the first and for both of them to
145*4882a593Smuzhiyun   be managed by the same daemon.  For the daemon to be able to mount
146*4882a593Smuzhiyun   something on the second it must be able to "walk" down past the
147*4882a593Smuzhiyun   first.  This means that d_manage cannot *always* return -EISDIR for
148*4882a593Smuzhiyun   the automount daemon.  It must only return it when a mount has
149*4882a593Smuzhiyun   been requested, but has not yet completed.
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun   `d_manage` also returns `-EISDIR` if the dentry shouldn't be a
152*4882a593Smuzhiyun   mount trap, either because it is a symbolic link or because it is
153*4882a593Smuzhiyun   not empty.
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun-  Any other negative value is treated as an error and returned
156*4882a593Smuzhiyun   to the caller.
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun   autofs can return
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun   - -ENOENT if the automount daemon failed to mount anything,
161*4882a593Smuzhiyun   - -ENOMEM if it ran out of memory,
162*4882a593Smuzhiyun   - -EINTR if a signal arrived while waiting for expiry to
163*4882a593Smuzhiyun     complete
164*4882a593Smuzhiyun   - or any other error sent down by the automount daemon.
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun
167*4882a593SmuzhiyunThe second use case only occurs during an "RCU-walk" and so `rcu_walk`
168*4882a593Smuzhiyunwill be set.
169*4882a593Smuzhiyun
170*4882a593SmuzhiyunAn RCU-walk is a fast and lightweight process for walking down a
171*4882a593Smuzhiyunfilename path (i.e. it is like running on tip-toes).  RCU-walk cannot
172*4882a593Smuzhiyuncope with all situations so when it finds a difficulty it falls back
173*4882a593Smuzhiyunto "REF-walk", which is slower but more robust.
174*4882a593Smuzhiyun
175*4882a593SmuzhiyunRCU-walk will never call `->d_automount`; the filesystems must already
176*4882a593Smuzhiyunbe mounted or RCU-walk cannot handle the path.
177*4882a593SmuzhiyunTo determine if a mount-trap is safe for RCU-walk mode it calls
178*4882a593Smuzhiyun`->d_manage()` with `rcu_walk` set to `true`.
179*4882a593Smuzhiyun
180*4882a593SmuzhiyunIn this case `d_manage()` must avoid blocking and should avoid taking
181*4882a593Smuzhiyunspinlocks if at all possible.  Its sole purpose is to determine if it
182*4882a593Smuzhiyunwould be safe to follow down into any mounted directory and the only
183*4882a593Smuzhiyunreason that it might not be is if an expiry of the mount is
184*4882a593Smuzhiyununderway.
185*4882a593Smuzhiyun
186*4882a593SmuzhiyunIn the `rcu_walk` case, `d_manage()` cannot return -EISDIR to tell the
187*4882a593SmuzhiyunVFS that this is a directory that doesn't require d_automount.  If
188*4882a593Smuzhiyun`rcu_walk` sees a dentry with DCACHE_NEED_AUTOMOUNT set but nothing
189*4882a593Smuzhiyunmounted, it *will* fall back to REF-walk.  `d_manage()` cannot make the
190*4882a593SmuzhiyunVFS remain in RCU-walk mode, but can only tell it to get out of
191*4882a593SmuzhiyunRCU-walk mode by returning `-ECHILD`.
192*4882a593Smuzhiyun
193*4882a593SmuzhiyunSo `d_manage()`, when called with `rcu_walk` set, should either return
194*4882a593Smuzhiyun-ECHILD if there is any reason to believe it is unsafe to enter the
195*4882a593Smuzhiyunmounted filesystem, otherwise it should return 0.
196*4882a593Smuzhiyun
197*4882a593Smuzhiyunautofs will return `-ECHILD` if an expiry of the filesystem has been
198*4882a593Smuzhiyuninitiated or is being considered, otherwise it returns 0.
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun
201*4882a593SmuzhiyunMountpoint expiry
202*4882a593Smuzhiyun=================
203*4882a593Smuzhiyun
204*4882a593SmuzhiyunThe VFS has a mechanism for automatically expiring unused mounts,
205*4882a593Smuzhiyunmuch as it can expire any unused dentry information from the dcache.
206*4882a593SmuzhiyunThis is guided by the MNT_SHRINKABLE flag.  This only applies to
207*4882a593Smuzhiyunmounts that were created by `d_automount()` returning a filesystem to be
208*4882a593Smuzhiyunmounted.  As autofs doesn't return such a filesystem but leaves the
209*4882a593Smuzhiyunmounting to the automount daemon, it must involve the automount daemon
210*4882a593Smuzhiyunin unmounting as well.  This also means that autofs has more control
211*4882a593Smuzhiyunover expiry.
212*4882a593Smuzhiyun
213*4882a593SmuzhiyunThe VFS also supports "expiry" of mounts using the MNT_EXPIRE flag to
214*4882a593Smuzhiyunthe `umount` system call.  Unmounting with MNT_EXPIRE will fail unless
215*4882a593Smuzhiyuna previous attempt had been made, and the filesystem has been inactive
216*4882a593Smuzhiyunand untouched since that previous attempt.  autofs does not depend on
217*4882a593Smuzhiyunthis but has its own internal tracking of whether filesystems were
218*4882a593Smuzhiyunrecently used.  This allows individual names in the autofs directory
219*4882a593Smuzhiyunto expire separately.
220*4882a593Smuzhiyun
221*4882a593SmuzhiyunWith version 4 of the protocol, the automount daemon can try to
222*4882a593Smuzhiyununmount any filesystems mounted on the autofs filesystem or remove any
223*4882a593Smuzhiyunsymbolic links or empty directories any time it likes.  If the unmount
224*4882a593Smuzhiyunor removal is successful the filesystem will be returned to the state
225*4882a593Smuzhiyunit was before the mount or creation, so that any access of the name
226*4882a593Smuzhiyunwill trigger normal auto-mount processing.  In particular, `rmdir` and
227*4882a593Smuzhiyun`unlink` do not leave negative entries in the dcache as a normal
228*4882a593Smuzhiyunfilesystem would, so an attempt to access a recently-removed object is
229*4882a593Smuzhiyunpassed to autofs for handling.
230*4882a593Smuzhiyun
231*4882a593SmuzhiyunWith version 5, this is not safe except for unmounting from top-level
232*4882a593Smuzhiyundirectories.  As lower-level directories are never mount traps, other
233*4882a593Smuzhiyunprocesses will see an empty directory as soon as the filesystem is
234*4882a593Smuzhiyununmounted.  So it is generally safest to use the autofs expiry
235*4882a593Smuzhiyunprotocol described below.
236*4882a593Smuzhiyun
237*4882a593SmuzhiyunNormally the daemon only wants to remove entries which haven't been
238*4882a593Smuzhiyunused for a while.  For this purpose autofs maintains a "`last_used`"
239*4882a593Smuzhiyuntime stamp on each directory or symlink.  For symlinks it genuinely
240*4882a593Smuzhiyundoes record the last time the symlink was "used" or followed to find
241*4882a593Smuzhiyunout where it points to.  For directories the field is used slightly
242*4882a593Smuzhiyundifferently.  The field is updated at mount time and during expire
243*4882a593Smuzhiyunchecks if it is found to be in use (ie. open file descriptor or
244*4882a593Smuzhiyunprocess working directory) and during path walks. The update done
245*4882a593Smuzhiyunduring path walks prevents frequent expire and immediate mount of
246*4882a593Smuzhiyunfrequently accessed automounts. But in the case where a GUI continually
247*4882a593Smuzhiyunaccess or an application frequently scans an autofs directory tree
248*4882a593Smuzhiyunthere can be an accumulation of mounts that aren't actually being
249*4882a593Smuzhiyunused. To cater for this case the "`strictexpire`" autofs mount option
250*4882a593Smuzhiyuncan be used to avoid the "`last_used`" update on path walk thereby
251*4882a593Smuzhiyunpreventing this apparent inability to expire mounts that aren't
252*4882a593Smuzhiyunreally in use.
253*4882a593Smuzhiyun
254*4882a593SmuzhiyunThe daemon is able to ask autofs if anything is due to be expired,
255*4882a593Smuzhiyunusing an `ioctl` as discussed later.  For a *direct* mount, autofs
256*4882a593Smuzhiyunconsiders if the entire mount-tree can be unmounted or not.  For an
257*4882a593Smuzhiyun*indirect* mount, autofs considers each of the names in the top level
258*4882a593Smuzhiyundirectory to determine if any of those can be unmounted and cleaned
259*4882a593Smuzhiyunup.
260*4882a593Smuzhiyun
261*4882a593SmuzhiyunThere is an option with indirect mounts to consider each of the leaves
262*4882a593Smuzhiyunthat has been mounted on instead of considering the top-level names.
263*4882a593SmuzhiyunThis was originally intended for compatibility with version 4 of autofs
264*4882a593Smuzhiyunand should be considered as deprecated for Sun Format automount maps.
265*4882a593SmuzhiyunHowever, it may be used again for amd format mount maps (which are
266*4882a593Smuzhiyungenerally indirect maps) because the amd automounter allows for the
267*4882a593Smuzhiyunsetting of an expire timeout for individual mounts. But there are
268*4882a593Smuzhiyunsome difficulties in making the needed changes for this.
269*4882a593Smuzhiyun
270*4882a593SmuzhiyunWhen autofs considers a directory it checks the `last_used` time and
271*4882a593Smuzhiyuncompares it with the "timeout" value set when the filesystem was
272*4882a593Smuzhiyunmounted, though this check is ignored in some cases. It also checks if
273*4882a593Smuzhiyunthe directory or anything below it is in use.  For symbolic links,
274*4882a593Smuzhiyunonly the `last_used` time is ever considered.
275*4882a593Smuzhiyun
276*4882a593SmuzhiyunIf both appear to support expiring the directory or symlink, an action
277*4882a593Smuzhiyunis taken.
278*4882a593Smuzhiyun
279*4882a593SmuzhiyunThere are two ways to ask autofs to consider expiry.  The first is to
280*4882a593Smuzhiyunuse the **AUTOFS_IOC_EXPIRE** ioctl.  This only works for indirect
281*4882a593Smuzhiyunmounts.  If it finds something in the root directory to expire it will
282*4882a593Smuzhiyunreturn the name of that thing.  Once a name has been returned the
283*4882a593Smuzhiyunautomount daemon needs to unmount any filesystems mounted below the
284*4882a593Smuzhiyunname normally.  As described above, this is unsafe for non-toplevel
285*4882a593Smuzhiyunmounts in a version-5 autofs.  For this reason the current `automount(8)`
286*4882a593Smuzhiyundoes not use this ioctl.
287*4882a593Smuzhiyun
288*4882a593SmuzhiyunThe second mechanism uses either the **AUTOFS_DEV_IOCTL_EXPIRE_CMD** or
289*4882a593Smuzhiyunthe **AUTOFS_IOC_EXPIRE_MULTI** ioctl.  This will work for both direct and
290*4882a593Smuzhiyunindirect mounts.  If it selects an object to expire, it will notify
291*4882a593Smuzhiyunthe daemon using the notification mechanism described below.  This
292*4882a593Smuzhiyunwill block until the daemon acknowledges the expiry notification.
293*4882a593SmuzhiyunThis implies that the "`EXPIRE`" ioctl must be sent from a different
294*4882a593Smuzhiyunthread than the one which handles notification.
295*4882a593Smuzhiyun
296*4882a593SmuzhiyunWhile the ioctl is blocking, the entry is marked as "expiring" and
297*4882a593Smuzhiyun`d_manage` will block until the daemon affirms that the unmount has
298*4882a593Smuzhiyuncompleted (together with removing any directories that might have been
299*4882a593Smuzhiyunnecessary), or has been aborted.
300*4882a593Smuzhiyun
301*4882a593SmuzhiyunCommunicating with autofs: detecting the daemon
302*4882a593Smuzhiyun===============================================
303*4882a593Smuzhiyun
304*4882a593SmuzhiyunThere are several forms of communication between the automount daemon
305*4882a593Smuzhiyunand the filesystem.  As we have already seen, the daemon can create and
306*4882a593Smuzhiyunremove directories and symlinks using normal filesystem operations.
307*4882a593Smuzhiyunautofs knows whether a process requesting some operation is the daemon
308*4882a593Smuzhiyunor not based on its process-group id number (see getpgid(1)).
309*4882a593Smuzhiyun
310*4882a593SmuzhiyunWhen an autofs filesystem is mounted the pgid of the mounting
311*4882a593Smuzhiyunprocesses is recorded unless the "pgrp=" option is given, in which
312*4882a593Smuzhiyuncase that number is recorded instead.  Any request arriving from a
313*4882a593Smuzhiyunprocess in that process group is considered to come from the daemon.
314*4882a593SmuzhiyunIf the daemon ever has to be stopped and restarted a new pgid can be
315*4882a593Smuzhiyunprovided through an ioctl as will be described below.
316*4882a593Smuzhiyun
317*4882a593SmuzhiyunCommunicating with autofs: the event pipe
318*4882a593Smuzhiyun=========================================
319*4882a593Smuzhiyun
320*4882a593SmuzhiyunWhen an autofs filesystem is mounted, the 'write' end of a pipe must
321*4882a593Smuzhiyunbe passed using the 'fd=' mount option.  autofs will write
322*4882a593Smuzhiyunnotification messages to this pipe for the daemon to respond to.
323*4882a593SmuzhiyunFor version 5, the format of the message is::
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun	struct autofs_v5_packet {
326*4882a593Smuzhiyun		struct autofs_packet_hdr hdr;
327*4882a593Smuzhiyun		autofs_wqt_t wait_queue_token;
328*4882a593Smuzhiyun		__u32 dev;
329*4882a593Smuzhiyun		__u64 ino;
330*4882a593Smuzhiyun		__u32 uid;
331*4882a593Smuzhiyun		__u32 gid;
332*4882a593Smuzhiyun		__u32 pid;
333*4882a593Smuzhiyun		__u32 tgid;
334*4882a593Smuzhiyun		__u32 len;
335*4882a593Smuzhiyun		char name[NAME_MAX+1];
336*4882a593Smuzhiyun        };
337*4882a593Smuzhiyun
338*4882a593SmuzhiyunAnd the format of the header is::
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun	struct autofs_packet_hdr {
341*4882a593Smuzhiyun		int proto_version;		/* Protocol version */
342*4882a593Smuzhiyun		int type;			/* Type of packet */
343*4882a593Smuzhiyun	};
344*4882a593Smuzhiyun
345*4882a593Smuzhiyunwhere the type is one of ::
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun	autofs_ptype_missing_indirect
348*4882a593Smuzhiyun	autofs_ptype_expire_indirect
349*4882a593Smuzhiyun	autofs_ptype_missing_direct
350*4882a593Smuzhiyun	autofs_ptype_expire_direct
351*4882a593Smuzhiyun
352*4882a593Smuzhiyunso messages can indicate that a name is missing (something tried to
353*4882a593Smuzhiyunaccess it but it isn't there) or that it has been selected for expiry.
354*4882a593Smuzhiyun
355*4882a593SmuzhiyunThe pipe will be set to "packet mode" (equivalent to passing
356*4882a593Smuzhiyun`O_DIRECT`) to _pipe2(2)_ so that a read from the pipe will return at
357*4882a593Smuzhiyunmost one packet, and any unread portion of a packet will be discarded.
358*4882a593Smuzhiyun
359*4882a593SmuzhiyunThe `wait_queue_token` is a unique number which can identify a
360*4882a593Smuzhiyunparticular request to be acknowledged.  When a message is sent over
361*4882a593Smuzhiyunthe pipe the affected dentry is marked as either "active" or
362*4882a593Smuzhiyun"expiring" and other accesses to it block until the message is
363*4882a593Smuzhiyunacknowledged using one of the ioctls below with the relevant
364*4882a593Smuzhiyun`wait_queue_token`.
365*4882a593Smuzhiyun
366*4882a593SmuzhiyunCommunicating with autofs: root directory ioctls
367*4882a593Smuzhiyun================================================
368*4882a593Smuzhiyun
369*4882a593SmuzhiyunThe root directory of an autofs filesystem will respond to a number of
370*4882a593Smuzhiyunioctls.  The process issuing the ioctl must have the CAP_SYS_ADMIN
371*4882a593Smuzhiyuncapability, or must be the automount daemon.
372*4882a593Smuzhiyun
373*4882a593SmuzhiyunThe available ioctl commands are:
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun- **AUTOFS_IOC_READY**:
376*4882a593Smuzhiyun	a notification has been handled.  The argument
377*4882a593Smuzhiyun	to the ioctl command is the "wait_queue_token" number
378*4882a593Smuzhiyun	corresponding to the notification being acknowledged.
379*4882a593Smuzhiyun- **AUTOFS_IOC_FAIL**:
380*4882a593Smuzhiyun	similar to above, but indicates failure with
381*4882a593Smuzhiyun	the error code `ENOENT`.
382*4882a593Smuzhiyun- **AUTOFS_IOC_CATATONIC**:
383*4882a593Smuzhiyun	Causes the autofs to enter "catatonic"
384*4882a593Smuzhiyun	mode meaning that it stops sending notifications to the daemon.
385*4882a593Smuzhiyun	This mode is also entered if a write to the pipe fails.
386*4882a593Smuzhiyun- **AUTOFS_IOC_PROTOVER**:
387*4882a593Smuzhiyun	This returns the protocol version in use.
388*4882a593Smuzhiyun- **AUTOFS_IOC_PROTOSUBVER**:
389*4882a593Smuzhiyun	Returns the protocol sub-version which
390*4882a593Smuzhiyun	is really a version number for the implementation.
391*4882a593Smuzhiyun- **AUTOFS_IOC_SETTIMEOUT**:
392*4882a593Smuzhiyun	This passes a pointer to an unsigned
393*4882a593Smuzhiyun	long.  The value is used to set the timeout for expiry, and
394*4882a593Smuzhiyun	the current timeout value is stored back through the pointer.
395*4882a593Smuzhiyun- **AUTOFS_IOC_ASKUMOUNT**:
396*4882a593Smuzhiyun	Returns, in the pointed-to `int`, 1 if
397*4882a593Smuzhiyun	the filesystem could be unmounted.  This is only a hint as
398*4882a593Smuzhiyun	the situation could change at any instant.  This call can be
399*4882a593Smuzhiyun	used to avoid a more expensive full unmount attempt.
400*4882a593Smuzhiyun- **AUTOFS_IOC_EXPIRE**:
401*4882a593Smuzhiyun	as described above, this asks if there is
402*4882a593Smuzhiyun	anything suitable to expire.  A pointer to a packet::
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun		struct autofs_packet_expire_multi {
405*4882a593Smuzhiyun			struct autofs_packet_hdr hdr;
406*4882a593Smuzhiyun			autofs_wqt_t wait_queue_token;
407*4882a593Smuzhiyun			int len;
408*4882a593Smuzhiyun			char name[NAME_MAX+1];
409*4882a593Smuzhiyun		};
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun	is required.  This is filled in with the name of something
412*4882a593Smuzhiyun	that can be unmounted or removed.  If nothing can be expired,
413*4882a593Smuzhiyun	`errno` is set to `EAGAIN`.  Even though a `wait_queue_token`
414*4882a593Smuzhiyun	is present in the structure, no "wait queue" is established
415*4882a593Smuzhiyun	and no acknowledgment is needed.
416*4882a593Smuzhiyun- **AUTOFS_IOC_EXPIRE_MULTI**:
417*4882a593Smuzhiyun	This is similar to
418*4882a593Smuzhiyun	**AUTOFS_IOC_EXPIRE** except that it causes notification to be
419*4882a593Smuzhiyun	sent to the daemon, and it blocks until the daemon acknowledges.
420*4882a593Smuzhiyun	The argument is an integer which can contain two different flags.
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun	**AUTOFS_EXP_IMMEDIATE** causes `last_used` time to be ignored
423*4882a593Smuzhiyun	and objects are expired if the are not in use.
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun	**AUTOFS_EXP_FORCED** causes the in use status to be ignored
426*4882a593Smuzhiyun	and objects are expired ieven if they are in use. This assumes
427*4882a593Smuzhiyun	that the daemon has requested this because it is capable of
428*4882a593Smuzhiyun	performing the umount.
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun	**AUTOFS_EXP_LEAVES** will select a leaf rather than a top-level
431*4882a593Smuzhiyun	name to expire.  This is only safe when *maxproto* is 4.
432*4882a593Smuzhiyun
433*4882a593SmuzhiyunCommunicating with autofs: char-device ioctls
434*4882a593Smuzhiyun=============================================
435*4882a593Smuzhiyun
436*4882a593SmuzhiyunIt is not always possible to open the root of an autofs filesystem,
437*4882a593Smuzhiyunparticularly a *direct* mounted filesystem.  If the automount daemon
438*4882a593Smuzhiyunis restarted there is no way for it to regain control of existing
439*4882a593Smuzhiyunmounts using any of the above communication channels.  To address this
440*4882a593Smuzhiyunneed there is a "miscellaneous" character device (major 10, minor 235)
441*4882a593Smuzhiyunwhich can be used to communicate directly with the autofs filesystem.
442*4882a593SmuzhiyunIt requires CAP_SYS_ADMIN for access.
443*4882a593Smuzhiyun
444*4882a593SmuzhiyunThe 'ioctl's that can be used on this device are described in a separate
445*4882a593Smuzhiyundocument `autofs-mount-control.txt`, and are summarised briefly here.
446*4882a593SmuzhiyunEach ioctl is passed a pointer to an `autofs_dev_ioctl` structure::
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun        struct autofs_dev_ioctl {
449*4882a593Smuzhiyun                __u32 ver_major;
450*4882a593Smuzhiyun                __u32 ver_minor;
451*4882a593Smuzhiyun                __u32 size;             /* total size of data passed in
452*4882a593Smuzhiyun                                         * including this struct */
453*4882a593Smuzhiyun                __s32 ioctlfd;          /* automount command fd */
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun		/* Command parameters */
456*4882a593Smuzhiyun		union {
457*4882a593Smuzhiyun			struct args_protover		protover;
458*4882a593Smuzhiyun			struct args_protosubver		protosubver;
459*4882a593Smuzhiyun			struct args_openmount		openmount;
460*4882a593Smuzhiyun			struct args_ready		ready;
461*4882a593Smuzhiyun			struct args_fail		fail;
462*4882a593Smuzhiyun			struct args_setpipefd		setpipefd;
463*4882a593Smuzhiyun			struct args_timeout		timeout;
464*4882a593Smuzhiyun			struct args_requester		requester;
465*4882a593Smuzhiyun			struct args_expire		expire;
466*4882a593Smuzhiyun			struct args_askumount		askumount;
467*4882a593Smuzhiyun			struct args_ismountpoint	ismountpoint;
468*4882a593Smuzhiyun		};
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun                char path[0];
471*4882a593Smuzhiyun        };
472*4882a593Smuzhiyun
473*4882a593SmuzhiyunFor the **OPEN_MOUNT** and **IS_MOUNTPOINT** commands, the target
474*4882a593Smuzhiyunfilesystem is identified by the `path`.  All other commands identify
475*4882a593Smuzhiyunthe filesystem by the `ioctlfd` which is a file descriptor open on the
476*4882a593Smuzhiyunroot, and which can be returned by **OPEN_MOUNT**.
477*4882a593Smuzhiyun
478*4882a593SmuzhiyunThe `ver_major` and `ver_minor` are in/out parameters which check that
479*4882a593Smuzhiyunthe requested version is supported, and report the maximum version
480*4882a593Smuzhiyunthat the kernel module can support.
481*4882a593Smuzhiyun
482*4882a593SmuzhiyunCommands are:
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun- **AUTOFS_DEV_IOCTL_VERSION_CMD**:
485*4882a593Smuzhiyun	does nothing, except validate and
486*4882a593Smuzhiyun	set version numbers.
487*4882a593Smuzhiyun- **AUTOFS_DEV_IOCTL_OPENMOUNT_CMD**:
488*4882a593Smuzhiyun	return an open file descriptor
489*4882a593Smuzhiyun	on the root of an autofs filesystem.  The filesystem is identified
490*4882a593Smuzhiyun	by name and device number, which is stored in `openmount.devid`.
491*4882a593Smuzhiyun	Device numbers for existing filesystems can be found in
492*4882a593Smuzhiyun	`/proc/self/mountinfo`.
493*4882a593Smuzhiyun- **AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD**:
494*4882a593Smuzhiyun	same as `close(ioctlfd)`.
495*4882a593Smuzhiyun- **AUTOFS_DEV_IOCTL_SETPIPEFD_CMD**:
496*4882a593Smuzhiyun	if the filesystem is in
497*4882a593Smuzhiyun	catatonic mode, this can provide the write end of a new pipe
498*4882a593Smuzhiyun	in `setpipefd.pipefd` to re-establish communication with a daemon.
499*4882a593Smuzhiyun	The process group of the calling process is used to identify the
500*4882a593Smuzhiyun	daemon.
501*4882a593Smuzhiyun- **AUTOFS_DEV_IOCTL_REQUESTER_CMD**:
502*4882a593Smuzhiyun	`path` should be a
503*4882a593Smuzhiyun	name within the filesystem that has been auto-mounted on.
504*4882a593Smuzhiyun	On successful return, `requester.uid` and `requester.gid` will be
505*4882a593Smuzhiyun	the UID and GID of the process which triggered that mount.
506*4882a593Smuzhiyun- **AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD**:
507*4882a593Smuzhiyun	Check if path is a
508*4882a593Smuzhiyun	mountpoint of a particular type - see separate documentation for
509*4882a593Smuzhiyun	details.
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun- **AUTOFS_DEV_IOCTL_PROTOVER_CMD**
512*4882a593Smuzhiyun- **AUTOFS_DEV_IOCTL_PROTOSUBVER_CMD**
513*4882a593Smuzhiyun- **AUTOFS_DEV_IOCTL_READY_CMD**
514*4882a593Smuzhiyun- **AUTOFS_DEV_IOCTL_FAIL_CMD**
515*4882a593Smuzhiyun- **AUTOFS_DEV_IOCTL_CATATONIC_CMD**
516*4882a593Smuzhiyun- **AUTOFS_DEV_IOCTL_TIMEOUT_CMD**
517*4882a593Smuzhiyun- **AUTOFS_DEV_IOCTL_EXPIRE_CMD**
518*4882a593Smuzhiyun- **AUTOFS_DEV_IOCTL_ASKUMOUNT_CMD**
519*4882a593Smuzhiyun
520*4882a593SmuzhiyunThese all have the same
521*4882a593Smuzhiyunfunction as the similarly named **AUTOFS_IOC** ioctls, except
522*4882a593Smuzhiyunthat **FAIL** can be given an explicit error number in `fail.status`
523*4882a593Smuzhiyuninstead of assuming `ENOENT`, and this **EXPIRE** command
524*4882a593Smuzhiyuncorresponds to **AUTOFS_IOC_EXPIRE_MULTI**.
525*4882a593Smuzhiyun
526*4882a593SmuzhiyunCatatonic mode
527*4882a593Smuzhiyun==============
528*4882a593Smuzhiyun
529*4882a593SmuzhiyunAs mentioned, an autofs mount can enter "catatonic" mode.  This
530*4882a593Smuzhiyunhappens if a write to the notification pipe fails, or if it is
531*4882a593Smuzhiyunexplicitly requested by an `ioctl`.
532*4882a593Smuzhiyun
533*4882a593SmuzhiyunWhen entering catatonic mode, the pipe is closed and any pending
534*4882a593Smuzhiyunnotifications are acknowledged with the error `ENOENT`.
535*4882a593Smuzhiyun
536*4882a593SmuzhiyunOnce in catatonic mode attempts to access non-existing names will
537*4882a593Smuzhiyunresult in `ENOENT` while attempts to access existing directories will
538*4882a593Smuzhiyunbe treated in the same way as if they came from the daemon, so mount
539*4882a593Smuzhiyuntraps will not fire.
540*4882a593Smuzhiyun
541*4882a593SmuzhiyunWhen the filesystem is mounted a _uid_ and _gid_ can be given which
542*4882a593Smuzhiyunset the ownership of directories and symbolic links.  When the
543*4882a593Smuzhiyunfilesystem is in catatonic mode, any process with a matching UID can
544*4882a593Smuzhiyuncreate directories or symlinks in the root directory, but not in other
545*4882a593Smuzhiyundirectories.
546*4882a593Smuzhiyun
547*4882a593SmuzhiyunCatatonic mode can only be left via the
548*4882a593Smuzhiyun**AUTOFS_DEV_IOCTL_OPENMOUNT_CMD** ioctl on the `/dev/autofs`.
549*4882a593Smuzhiyun
550*4882a593SmuzhiyunThe "ignore" mount option
551*4882a593Smuzhiyun=========================
552*4882a593Smuzhiyun
553*4882a593SmuzhiyunThe "ignore" mount option can be used to provide a generic indicator
554*4882a593Smuzhiyunto applications that the mount entry should be ignored when displaying
555*4882a593Smuzhiyunmount information.
556*4882a593Smuzhiyun
557*4882a593SmuzhiyunIn other OSes that provide autofs and that provide a mount list to user
558*4882a593Smuzhiyunspace based on the kernel mount list a no-op mount option ("ignore" is
559*4882a593Smuzhiyunthe one use on the most common OSes) is allowed so that autofs file
560*4882a593Smuzhiyunsystem users can optionally use it.
561*4882a593Smuzhiyun
562*4882a593SmuzhiyunThis is intended to be used by user space programs to exclude autofs
563*4882a593Smuzhiyunmounts from consideration when reading the mounts list.
564*4882a593Smuzhiyun
565*4882a593Smuzhiyunautofs, name spaces, and shared mounts
566*4882a593Smuzhiyun======================================
567*4882a593Smuzhiyun
568*4882a593SmuzhiyunWith bind mounts and name spaces it is possible for an autofs
569*4882a593Smuzhiyunfilesystem to appear at multiple places in one or more filesystem
570*4882a593Smuzhiyunname spaces.  For this to work sensibly, the autofs filesystem should
571*4882a593Smuzhiyunalways be mounted "shared". e.g. ::
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun	mount --make-shared /autofs/mount/point
574*4882a593Smuzhiyun
575*4882a593SmuzhiyunThe automount daemon is only able to manage a single mount location for
576*4882a593Smuzhiyunan autofs filesystem and if mounts on that are not 'shared', other
577*4882a593Smuzhiyunlocations will not behave as expected.  In particular access to those
578*4882a593Smuzhiyunother locations will likely result in the `ELOOP` error ::
579*4882a593Smuzhiyun
580*4882a593Smuzhiyun	Too many levels of symbolic links
581