Whamcloud - gitweb
337c1b9fd4bc3cfbcd98abc1f651b665cf1f0cb9
[fs/lustre-release.git] / lustre / utils / ll_decode_linkea.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; if not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2016, DDN Storage Corporation.
24  */
25 /*
26  * lustre/utils/ll_decode_linkea.c
27  *
28  * Tool for printing the MDT link_ea structure on the objects
29  * in human readable form.
30  *
31  * Author: Li Xi <lixi@ddn.com>
32  */
33
34
35 #include <stdio.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <limits.h>
39 #include <sys/types.h>
40 #include <sys/xattr.h>
41 #include <lustre/lustre_idl.h>
42 #include <lustre/lustre_user.h>
43
44 #define BUFFER_SIZE 65536
45
46 int decode_linkea(const char *fname)
47 {
48         char buf[BUFFER_SIZE];
49         struct link_ea_header *leh;
50         ssize_t size;
51         struct link_ea_entry *lee;
52         int i;
53         __u64 length;
54         int reclen;
55         struct lu_fid pfid;
56
57         size = getxattr(fname, "trusted.link", buf, BUFFER_SIZE);
58         if (size < 0) {
59                 if (errno == ERANGE) {
60                         fprintf(stderr, "%s: failed to read trusted.link "
61                                 "xattr, the buffer size %u might be too "
62                                 "small\n", fname, BUFFER_SIZE);
63                 } else {
64                         fprintf(stderr,
65                                 "%s: failed to read trusted.link xattr: %s\n",
66                                 fname, strerror(errno));
67                 }
68                 return -1;
69         }
70
71         leh = (struct link_ea_header *)buf;
72         if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
73                 leh->leh_magic = LINK_EA_MAGIC;
74                 leh->leh_reccount = __swab32(leh->leh_reccount);
75                 leh->leh_len = __swab64(leh->leh_len);
76         }
77         if (leh->leh_magic != LINK_EA_MAGIC) {
78                 fprintf(stderr,
79                         "%s: magic mismatch, expected 0x%lx, got 0x%x\n",
80                         fname, LINK_EA_MAGIC, leh->leh_magic);
81                 return -1;
82         }
83         if (leh->leh_reccount == 0) {
84                 fprintf(stderr, "%s: empty record count\n", fname);
85                 return -1;
86         }
87         if (leh->leh_len > size) {
88                 fprintf(stderr,
89                         "%s: invalid length %llu, should smaller than %zd\n",
90                         fname, leh->leh_len, size);
91                 return -1;
92         }
93
94         length = sizeof(struct link_ea_header);
95         lee = (struct link_ea_entry *)(leh + 1);
96         printf("%s: count %u\n", fname, leh->leh_reccount);
97         for (i = 0; i < leh->leh_reccount; i++) {
98                 reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
99                 length += reclen;
100                 if (length > leh->leh_len) {
101                         fprintf(stderr,
102                                 "%s: length exceeded, expected %lld, got %lld\n",
103                                 fname, leh->leh_len, length);
104                         return -1;
105                 }
106                 memcpy(&pfid, &lee->lee_parent_fid, sizeof(pfid));
107                 fid_be_to_cpu(&pfid, &pfid);
108
109                 printf("    %d: pfid "DFID", name '%s'\n", i, PFID(&pfid),
110                        lee->lee_name);
111                 lee = (struct link_ea_entry *)((char *)lee + reclen);
112         }
113
114         if (length != leh->leh_len) {
115                 fprintf(stderr,
116                         "%s: length mismatch, expected %lld, got %lld\n",
117                         fname, leh->leh_len, length);
118                 return -1;
119         }
120
121         return 0;
122 }
123
124 int main(int argc, char *argv[])
125 {
126         int     rc = 0;
127         int     rc2;
128         int     i;
129
130         for (i = 1; i < argc; i++) {
131                 rc2 = decode_linkea(argv[i]);
132                 if (rc2 != 0)
133                         rc = rc2;
134         }
135
136         return rc;
137 }