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.
352 points outside your accessible address space.
355 .I handle\->handle_bytes
362 refers to a symbolic link, but
368 The caller does not have the
369 .BR CAP_DAC_READ_SEARCH
376 This error will occur if, for example, the file has been deleted.
378 These system calls first appeared in Linux 2.6.39.
379 Library support is provided in glibc since version 2.14.
381 These system calls are nonstandard Linux extensions.
383 FreeBSD has a broadly similar pair of system calls in the form of
388 A file handle can be generated in one process using
389 .BR name_to_handle_at ()
390 and later used in a different process that calls
391 .BR open_by_handle_at ().
393 Some filesystem don't support the translation of pathnames to
394 file handles, for example,
397 and various network filesystems.
399 A file handle may become invalid ("stale") if a file is deleted,
400 or for other filesystem-specific reasons.
401 Invalid handles are notified by an
404 .BR open_by_handle_at ().
406 These system calls are designed for use by user-space file servers.
407 For example, a user-space NFS server might generate a file handle
408 and pass it to an NFS client.
409 Later, when the client wants to open the file,
410 it could pass the handle back to the server.
411 .\" https://lwn.net/Articles/375888/
412 .\" "Open by handle" - Jonathan Corbet, 2010-02-23
413 This sort of functionality allows a user-space file server to operate in
414 a stateless fashion with respect to the files it serves.
418 refers to a symbolic link and
421 .BR AT_SYMLINK_FOLLOW ,
423 .BR name_to_handle_at ()
424 returns a handle for the link (rather than the file to which it refers).
425 .\" commit bcda76524cd1fa32af748536f27f674a13e56700
426 The process receiving the handle can later perform operations
427 on the symbolic link by converting the handle to a file descriptor using
428 .BR open_by_handle_at ()
431 flag, and then passing the file descriptor as the
433 argument in system calls such as
437 .SS Obtaining a persistent filesystem ID
439 .IR /proc/self/mountinfo
440 can be reused as filesystems are unmounted and mounted.
441 Therefore, the mount ID returned by
442 .BR name_to_handle_at ()
445 should not be treated as a persistent identifier
446 for the corresponding mounted filesystem.
447 However, an application can use the information in the
449 record that corresponds to the mount ID
450 to derive a persistent identifier.
452 For example, one can use the device name in the fifth field of the
454 record to search for the corresponding device UUID via the symbolic links in
455 .IR /dev/disks/by\-uuid .
456 (A more comfortable way of obtaining the UUID is to use the
457 .\" e.g., http://stackoverflow.com/questions/6748429/using-libblkid-to-find-uuid-of-a-partition
460 That process can then be reversed,
461 using the UUID to look up the device name,
462 and then obtaining the corresponding mount point,
463 in order to produce the
466 .BR open_by_handle_at ().
468 The two programs below demonstrate the use of
469 .BR name_to_handle_at ()
471 .BR open_by_handle_at ().
473 .RI ( t_name_to_handle_at.c )
475 .BR name_to_handle_at ()
476 to obtain the file handle and mount ID
477 for the file specified in its command-line argument;
478 the handle and mount ID are written to standard output.
481 .RI ( t_open_by_handle_at.c )
482 reads a mount ID and file handle from standard input.
483 The program then employs
484 .BR open_by_handle_at ()
485 to open the file using that handle.
486 If an optional command-line argument is supplied, then the
489 .BR open_by_handle_at ()
490 is obtained by opening the directory named in that argument.
493 is obtained by scanning
494 .IR /proc/self/mountinfo
495 to find a record whose mount ID matches the mount ID
496 read from standard input,
497 and the mount directory specified in that record is opened.
498 (These programs do not deal with the fact that mount IDs are not persistent.)
500 The following shell session demonstrates the use of these two programs:
504 $ \fBecho \(aqCan you please think about it?\(aq > cecilia.txt\fP
505 $ \fB./t_name_to_handle_at cecilia.txt > fh\fP
506 $ \fB./t_open_by_handle_at < fh\fP
507 open_by_handle_at: Operation not permitted
508 $ \fBsudo ./t_open_by_handle_at < fh\fP # Need CAP_SYS_ADMIN
510 $ \fBrm cecilia.txt\fP
514 Now we delete and (quickly) re-create the file so that
515 it has the same content and (by chance) the same inode.
517 .BR open_by_handle_at ()
518 .\" Christoph Hellwig: That's why the file handles contain a generation
519 .\" counter that gets incremented in this case.
520 recognizes that the original file referred to by the file handle
525 $ \fBstat \-\-printf="%i\en" cecilia.txt\fP # Display inode number
527 $ \fBrm cecilia.txt\fP
528 $ \fBecho \(aqCan you please think about it?\(aq > cecilia.txt\fP
529 $ \fBstat \-\-printf="%i\en" cecilia.txt\fP # Check inode number
531 $ \fBsudo ./t_open_by_handle_at < fh\fP
532 open_by_handle_at: Stale NFS file handle
535 .SS Program source: t_name_to_handle_at.c
539 #include <sys/types.h>
540 #include <sys/stat.h>
548 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
552 main(int argc, char *argv[])
554 struct file_handle *fhp;
555 int mount_id, fhsize, flags, dirfd;
559 fprintf(stderr, "Usage: %s pathname\en", argv[0]);
565 /* Allocate file_handle structure. */
567 fhsize = sizeof(*fhp);
568 fhp = malloc(fhsize);
572 /* Make an initial call to name_to_handle_at() to discover
573 the size required for file handle. */
575 dirfd = AT_FDCWD; /* For name_to_handle_at() calls */
576 flags = 0; /* For name_to_handle_at() calls */
577 fhp\->handle_bytes = 0;
578 if (name_to_handle_at(dirfd, pathname, fhp,
579 &mount_id, flags) != \-1 || errno != EOVERFLOW) {
580 fprintf(stderr, "Unexpected result from name_to_handle_at()\en");
584 /* Reallocate file_handle structure with correct size. */
586 fhsize = sizeof(*fhp) + fhp\->handle_bytes;
587 fhp = realloc(fhp, fhsize); /* Copies fhp\->handle_bytes */
591 /* Get file handle from pathname supplied on command line. */
593 if (name_to_handle_at(dirfd, pathname, fhp, &mount_id, flags) == \-1)
594 errExit("name_to_handle_at");
596 /* Write mount ID, file handle size, and file handle to stdout,
597 for later reuse by t_open_by_handle_at.c. */
599 printf("%d\en", mount_id);
600 printf("%u %d ", fhp\->handle_bytes, fhp\->handle_type);
601 for (int j = 0; j < fhp\->handle_bytes; j++)
602 printf(" %02x", fhp\->f_handle[j]);
608 .SS Program source: t_open_by_handle_at.c
612 #include <sys/types.h>
613 #include <sys/stat.h>
621 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
624 /* Scan /proc/self/mountinfo to find the line whose mount ID matches
625 \(aqmount_id\(aq. (An easier way to do this is to install and use the
626 \(aqlibmount\(aq library provided by the \(aqutil\-linux\(aq project.)
627 Open the corresponding mount path and return the resulting file
631 open_mount_path_by_id(int mount_id)
635 char mount_path[PATH_MAX];
636 int mi_mount_id, found;
640 fp = fopen("/proc/self/mountinfo", "r");
647 nread = getline(&linep, &lsize, fp);
651 nread = sscanf(linep, "%d %*d %*s %*s %s",
652 &mi_mount_id, mount_path);
654 fprintf(stderr, "Bad sscanf()\en");
658 if (mi_mount_id == mount_id)
666 fprintf(stderr, "Could not find mount point\en");
670 return open(mount_path, O_RDONLY);
674 main(int argc, char *argv[])
676 struct file_handle *fhp;
677 int mount_id, fd, mount_fd, handle_bytes;
680 #define LINE_SIZE 100
681 char line1[LINE_SIZE], line2[LINE_SIZE];
684 if ((argc > 1 && strcmp(argv[1], "\-\-help") == 0) || argc > 2) {
685 fprintf(stderr, "Usage: %s [mount\-path]\en", argv[0]);
689 /* Standard input contains mount ID and file handle information:
692 Line 2: <handle_bytes> <handle_type> <bytes of handle in hex>
695 if ((fgets(line1, sizeof(line1), stdin) == NULL) ||
696 (fgets(line2, sizeof(line2), stdin) == NULL)) {
697 fprintf(stderr, "Missing mount_id / file handle\en");
701 mount_id = atoi(line1);
703 handle_bytes = strtoul(line2, &nextp, 0);
705 /* Given handle_bytes, we can now allocate file_handle structure. */
707 fhp = malloc(sizeof(*fhp) + handle_bytes);
711 fhp\->handle_bytes = handle_bytes;
713 fhp\->handle_type = strtoul(nextp, &nextp, 0);
715 for (int j = 0; j < fhp\->handle_bytes; j++)
716 fhp\->f_handle[j] = strtoul(nextp, &nextp, 16);
718 /* Obtain file descriptor for mount point, either by opening
719 the pathname specified on the command line, or by scanning
720 /proc/self/mounts to find a mount that matches the \(aqmount_id\(aq
721 that we received from stdin. */
724 mount_fd = open(argv[1], O_RDONLY);
726 mount_fd = open_mount_path_by_id(mount_id);
729 errExit("opening mount fd");
731 /* Open file using handle and mount point. */
733 fd = open_by_handle_at(mount_fd, fhp, O_RDONLY);
735 errExit("open_by_handle_at");
737 /* Try reading a few bytes from the file. */
739 nread = read(fd, buf, sizeof(buf));
743 printf("Read %zd bytes\en", nread);
759 documentation in the latest
762 .UR https://www.kernel.org/pub/linux/utils/util\-linux/