1 .\" Copyright (c) 2014 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH OPEN_BY_HANDLE_AT 2 2021-03-22 "Linux" "Linux Programmer's Manual"
27 name_to_handle_at, open_by_handle_at \- obtain handle
28 for a pathname and open file via a handle
31 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
34 .BI "int name_to_handle_at(int " dirfd ", const char *" pathname ,
35 .BI " struct file_handle *" handle ,
36 .BI " int *" mount_id ", int " flags );
37 .BI "int open_by_handle_at(int " mount_fd ", struct file_handle *" handle ,
42 .BR name_to_handle_at ()
44 .BR open_by_handle_at ()
45 system calls split the functionality of
48 .BR name_to_handle_at ()
49 returns an opaque handle that corresponds to a specified file;
50 .BR open_by_handle_at ()
51 opens the file corresponding to a handle returned by a previous call to
52 .BR name_to_handle_at ()
53 and returns an open file descriptor.
56 .SS name_to_handle_at()
58 .BR name_to_handle_at ()
59 system call returns a file handle and a mount ID corresponding to
60 the file specified by the
65 The file handle is returned via the argument
67 which is a pointer to a structure of the following form:
72 unsigned int handle_bytes; /* Size of f_handle [in, out] */
73 int handle_type; /* Handle type [out] */
74 unsigned char f_handle[0]; /* File identifier (sized by
80 It is the caller's responsibility to allocate the structure
81 with a size large enough to hold the handle returned in
85 field should be initialized to contain the allocated size for
91 specifies the maximum expected size for a file handle.
93 guaranteed upper limit as future filesystems may require more space.)
94 Upon successful return, the
96 field is updated to contain the number of bytes actually written to
99 The caller can discover the required size for the
101 structure by making a call in which
102 .IR handle\->handle_bytes
104 in this case, the call fails with the error
107 .IR handle\->handle_bytes
108 is set to indicate the required size;
109 the caller can then use this information to allocate a structure
110 of the correct size (see EXAMPLES below).
111 Some care is needed here as
113 can also indicate that no file handle is available for this particular
114 name in a filesystem which does normally support file-handle lookup.
115 This case can be detected when the
117 error is returned without
121 Other than the use of the
123 field, the caller should treat the
125 structure as an opaque data type: the
129 fields are needed only by a subsequent call to
130 .BR open_by_handle_at ().
134 argument is a bit mask constructed by ORing together zero or more of
137 .BR AT_SYMLINK_FOLLOW ,
144 arguments identify the file for which a handle is to be obtained.
145 There are four distinct cases:
149 is a nonempty string containing an absolute pathname,
150 then a handle is returned for the file referred to by that pathname.
157 is a nonempty string containing a relative pathname and
159 has the special value
163 is interpreted relative to the current working directory of the caller,
164 and a handle is returned for the file to which it refers.
168 is a nonempty string containing a relative pathname and
170 is a file descriptor referring to a directory, then
172 is interpreted relative to the directory referred to by
174 and a handle is returned for the file to which it refers.
177 for an explanation of why "directory file descriptors" are useful.)
181 is an empty string and
187 can be an open file descriptor referring to any type of file,
190 meaning the current working directory,
191 and a handle is returned for the file to which it refers.
195 argument returns an identifier for the filesystem
196 mount that corresponds to
198 This corresponds to the first field in one of the records in
199 .IR /proc/self/mountinfo .
200 Opening the pathname in the fifth field of that record yields a file
201 descriptor for the mount point;
202 that file descriptor can be used in a subsequent call to
203 .BR open_by_handle_at ().
205 is returned both for a successful call and for a call that results
210 .BR name_to_handle_at ()
213 if it is a symbolic link, and thus returns a handle for the link itself.
219 is dereferenced if it is a symbolic link
220 (so that the call returns a handle for the file referred to by the link).
222 .BR name_to_handle_at ()
223 does not trigger a mount when the final component of the pathname is an
225 When a filesystem supports both file handles and
227 .BR name_to_handle_at ()
228 call on an automount point will return with error
230 without having increased
232 This can happen since Linux 4.13
233 .\" commit 20fa19027286983ab2734b5910c4a687436e0c31
234 with NFS when accessing a directory
235 which is on a separate filesystem on the server.
236 In this case, the automount can be triggered by adding a "/" to the end
238 .SS open_by_handle_at()
240 .BR open_by_handle_at ()
241 system call opens the file referred to by
243 a file handle returned by a previous call to
244 .BR name_to_handle_at ().
248 argument is a file descriptor for any object (file, directory, etc.)
249 in the mounted filesystem with respect to which
251 should be interpreted.
254 can be specified, meaning the current working directory of the caller.
263 refers to a symbolic link, the caller must specify the
265 flag, and the symbolic link is not dereferenced; the
267 flag, if specified, is ignored.
269 The caller must have the
270 .B CAP_DAC_READ_SEARCH
272 .BR open_by_handle_at ().
275 .BR name_to_handle_at ()
278 .BR open_by_handle_at ()
279 returns a file descriptor (a nonnegative integer).
281 In the event of an error, both system calls return \-1 and set
283 to indicate the error.
285 .BR name_to_handle_at ()
287 .BR open_by_handle_at ()
288 can fail for the same errors as
290 In addition, they can fail with the errors noted below.
292 .BR name_to_handle_at ()
293 can fail with the following errors:
300 points outside your accessible address space.
304 includes an invalid bit value.
307 .IR handle\->handle_bytes
313 is an empty string, but
319 The file descriptor supplied in
321 does not refer to a directory,
322 and it is not the case that both
331 The filesystem does not support decoding of a pathname to a file handle.
335 .I handle\->handle_bytes
336 value passed into the call was too small.
337 When this error occurs,
338 .I handle\->handle_bytes
339 is updated to indicate the required size for the handle.
343 .BR open_by_handle_at ()
344 can fail with the following errors:
348 is not an open file descriptor.
354 is not a valid file descriptor.
358 points outside your accessible address space.
361 .I handle\->handle_bytes
368 refers to a symbolic link, but
374 The caller does not have the
375 .BR CAP_DAC_READ_SEARCH
382 This error will occur if, for example, the file has been deleted.
384 These system calls first appeared in Linux 2.6.39.
385 Library support is provided in glibc since version 2.14.
387 These system calls are nonstandard Linux extensions.
389 FreeBSD has a broadly similar pair of system calls in the form of
394 A file handle can be generated in one process using
395 .BR name_to_handle_at ()
396 and later used in a different process that calls
397 .BR open_by_handle_at ().
399 Some filesystem don't support the translation of pathnames to
400 file handles, for example,
403 and various network filesystems.
405 A file handle may become invalid ("stale") if a file is deleted,
406 or for other filesystem-specific reasons.
407 Invalid handles are notified by an
410 .BR open_by_handle_at ().
412 These system calls are designed for use by user-space file servers.
413 For example, a user-space NFS server might generate a file handle
414 and pass it to an NFS client.
415 Later, when the client wants to open the file,
416 it could pass the handle back to the server.
417 .\" https://lwn.net/Articles/375888/
418 .\" "Open by handle" - Jonathan Corbet, 2010-02-23
419 This sort of functionality allows a user-space file server to operate in
420 a stateless fashion with respect to the files it serves.
424 refers to a symbolic link and
427 .BR AT_SYMLINK_FOLLOW ,
429 .BR name_to_handle_at ()
430 returns a handle for the link (rather than the file to which it refers).
431 .\" commit bcda76524cd1fa32af748536f27f674a13e56700
432 The process receiving the handle can later perform operations
433 on the symbolic link by converting the handle to a file descriptor using
434 .BR open_by_handle_at ()
437 flag, and then passing the file descriptor as the
439 argument in system calls such as
443 .SS Obtaining a persistent filesystem ID
445 .IR /proc/self/mountinfo
446 can be reused as filesystems are unmounted and mounted.
447 Therefore, the mount ID returned by
448 .BR name_to_handle_at ()
451 should not be treated as a persistent identifier
452 for the corresponding mounted filesystem.
453 However, an application can use the information in the
455 record that corresponds to the mount ID
456 to derive a persistent identifier.
458 For example, one can use the device name in the fifth field of the
460 record to search for the corresponding device UUID via the symbolic links in
461 .IR /dev/disks/by\-uuid .
462 (A more comfortable way of obtaining the UUID is to use the
463 .\" e.g., http://stackoverflow.com/questions/6748429/using-libblkid-to-find-uuid-of-a-partition
466 That process can then be reversed,
467 using the UUID to look up the device name,
468 and then obtaining the corresponding mount point,
469 in order to produce the
472 .BR open_by_handle_at ().
474 The two programs below demonstrate the use of
475 .BR name_to_handle_at ()
477 .BR open_by_handle_at ().
479 .RI ( t_name_to_handle_at.c )
481 .BR name_to_handle_at ()
482 to obtain the file handle and mount ID
483 for the file specified in its command-line argument;
484 the handle and mount ID are written to standard output.
487 .RI ( t_open_by_handle_at.c )
488 reads a mount ID and file handle from standard input.
489 The program then employs
490 .BR open_by_handle_at ()
491 to open the file using that handle.
492 If an optional command-line argument is supplied, then the
495 .BR open_by_handle_at ()
496 is obtained by opening the directory named in that argument.
499 is obtained by scanning
500 .IR /proc/self/mountinfo
501 to find a record whose mount ID matches the mount ID
502 read from standard input,
503 and the mount directory specified in that record is opened.
504 (These programs do not deal with the fact that mount IDs are not persistent.)
506 The following shell session demonstrates the use of these two programs:
510 $ \fBecho \(aqCan you please think about it?\(aq > cecilia.txt\fP
511 $ \fB./t_name_to_handle_at cecilia.txt > fh\fP
512 $ \fB./t_open_by_handle_at < fh\fP
513 open_by_handle_at: Operation not permitted
514 $ \fBsudo ./t_open_by_handle_at < fh\fP # Need CAP_SYS_ADMIN
516 $ \fBrm cecilia.txt\fP
520 Now we delete and (quickly) re-create the file so that
521 it has the same content and (by chance) the same inode.
523 .BR open_by_handle_at ()
524 .\" Christoph Hellwig: That's why the file handles contain a generation
525 .\" counter that gets incremented in this case.
526 recognizes that the original file referred to by the file handle
531 $ \fBstat \-\-printf="%i\en" cecilia.txt\fP # Display inode number
533 $ \fBrm cecilia.txt\fP
534 $ \fBecho \(aqCan you please think about it?\(aq > cecilia.txt\fP
535 $ \fBstat \-\-printf="%i\en" cecilia.txt\fP # Check inode number
537 $ \fBsudo ./t_open_by_handle_at < fh\fP
538 open_by_handle_at: Stale NFS file handle
541 .SS Program source: t_name_to_handle_at.c
545 #include <sys/types.h>
546 #include <sys/stat.h>
554 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
558 main(int argc, char *argv[])
560 struct file_handle *fhp;
561 int mount_id, fhsize, flags, dirfd;
565 fprintf(stderr, "Usage: %s pathname\en", argv[0]);
571 /* Allocate file_handle structure. */
573 fhsize = sizeof(*fhp);
574 fhp = malloc(fhsize);
578 /* Make an initial call to name_to_handle_at() to discover
579 the size required for file handle. */
581 dirfd = AT_FDCWD; /* For name_to_handle_at() calls */
582 flags = 0; /* For name_to_handle_at() calls */
583 fhp\->handle_bytes = 0;
584 if (name_to_handle_at(dirfd, pathname, fhp,
585 &mount_id, flags) != \-1 || errno != EOVERFLOW) {
586 fprintf(stderr, "Unexpected result from name_to_handle_at()\en");
590 /* Reallocate file_handle structure with correct size. */
592 fhsize = sizeof(*fhp) + fhp\->handle_bytes;
593 fhp = realloc(fhp, fhsize); /* Copies fhp\->handle_bytes */
597 /* Get file handle from pathname supplied on command line. */
599 if (name_to_handle_at(dirfd, pathname, fhp, &mount_id, flags) == \-1)
600 errExit("name_to_handle_at");
602 /* Write mount ID, file handle size, and file handle to stdout,
603 for later reuse by t_open_by_handle_at.c. */
605 printf("%d\en", mount_id);
606 printf("%u %d ", fhp\->handle_bytes, fhp\->handle_type);
607 for (int j = 0; j < fhp\->handle_bytes; j++)
608 printf(" %02x", fhp\->f_handle[j]);
614 .SS Program source: t_open_by_handle_at.c
618 #include <sys/types.h>
619 #include <sys/stat.h>
627 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
630 /* Scan /proc/self/mountinfo to find the line whose mount ID matches
631 \(aqmount_id\(aq. (An easier way to do this is to install and use the
632 \(aqlibmount\(aq library provided by the \(aqutil\-linux\(aq project.)
633 Open the corresponding mount path and return the resulting file
637 open_mount_path_by_id(int mount_id)
641 char mount_path[PATH_MAX];
642 int mi_mount_id, found;
646 fp = fopen("/proc/self/mountinfo", "r");
653 nread = getline(&linep, &lsize, fp);
657 nread = sscanf(linep, "%d %*d %*s %*s %s",
658 &mi_mount_id, mount_path);
660 fprintf(stderr, "Bad sscanf()\en");
664 if (mi_mount_id == mount_id)
672 fprintf(stderr, "Could not find mount point\en");
676 return open(mount_path, O_RDONLY);
680 main(int argc, char *argv[])
682 struct file_handle *fhp;
683 int mount_id, fd, mount_fd, handle_bytes;
686 #define LINE_SIZE 100
687 char line1[LINE_SIZE], line2[LINE_SIZE];
690 if ((argc > 1 && strcmp(argv[1], "\-\-help") == 0) || argc > 2) {
691 fprintf(stderr, "Usage: %s [mount\-path]\en", argv[0]);
695 /* Standard input contains mount ID and file handle information:
698 Line 2: <handle_bytes> <handle_type> <bytes of handle in hex>
701 if ((fgets(line1, sizeof(line1), stdin) == NULL) ||
702 (fgets(line2, sizeof(line2), stdin) == NULL)) {
703 fprintf(stderr, "Missing mount_id / file handle\en");
707 mount_id = atoi(line1);
709 handle_bytes = strtoul(line2, &nextp, 0);
711 /* Given handle_bytes, we can now allocate file_handle structure. */
713 fhp = malloc(sizeof(*fhp) + handle_bytes);
717 fhp\->handle_bytes = handle_bytes;
719 fhp\->handle_type = strtoul(nextp, &nextp, 0);
721 for (int j = 0; j < fhp\->handle_bytes; j++)
722 fhp\->f_handle[j] = strtoul(nextp, &nextp, 16);
724 /* Obtain file descriptor for mount point, either by opening
725 the pathname specified on the command line, or by scanning
726 /proc/self/mounts to find a mount that matches the \(aqmount_id\(aq
727 that we received from stdin. */
730 mount_fd = open(argv[1], O_RDONLY);
732 mount_fd = open_mount_path_by_id(mount_id);
735 errExit("opening mount fd");
737 /* Open file using handle and mount point. */
739 fd = open_by_handle_at(mount_fd, fhp, O_RDONLY);
741 errExit("open_by_handle_at");
743 /* Try reading a few bytes from the file. */
745 nread = read(fd, buf, sizeof(buf));
749 printf("Read %zd bytes\en", nread);
765 documentation in the latest
768 .UR https://www.kernel.org/pub/linux/utils/util\-linux/