Whamcloud - gitweb
LU-11546 utils: enable large_dir for ldiskfs
[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 <linux/lustre/lustre_fid.h>
42
43 #define BUFFER_SIZE 65536
44
45 int decode_linkea(const char *fname)
46 {
47         char buf[BUFFER_SIZE];
48         struct link_ea_header *leh;
49         ssize_t size;
50         struct link_ea_entry *lee;
51         int i;
52         __u64 length;
53         int reclen;
54         struct lu_fid pfid;
55
56         size = getxattr(fname, "trusted.link", buf, BUFFER_SIZE);
57         if (size < 0) {
58                 if (errno == ERANGE) {
59                         fprintf(stderr, "%s: failed to read trusted.link "
60                                 "xattr, the buffer size %u might be too "
61                                 "small\n", fname, BUFFER_SIZE);
62                 } else {
63                         fprintf(stderr,
64                                 "%s: failed to read trusted.link xattr: %s\n",
65                                 fname, strerror(errno));
66                 }
67                 return -1;
68         }
69
70         leh = (struct link_ea_header *)buf;
71         if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
72                 leh->leh_magic = LINK_EA_MAGIC;
73                 leh->leh_reccount = __swab32(leh->leh_reccount);
74                 leh->leh_len = __swab64(leh->leh_len);
75         }
76         if (leh->leh_magic != LINK_EA_MAGIC) {
77                 fprintf(stderr,
78                         "%s: magic mismatch, expected 0x%lx, got 0x%x\n",
79                         fname, LINK_EA_MAGIC, leh->leh_magic);
80                 return -1;
81         }
82         if (leh->leh_reccount == 0) {
83                 fprintf(stderr, "%s: empty record count\n", fname);
84                 return -1;
85         }
86         if (leh->leh_len > size) {
87                 fprintf(stderr,
88                         "%s: invalid length %llu, should smaller than %zd\n",
89                         fname, (unsigned long long)leh->leh_len, size);
90                 return -1;
91         }
92
93         length = sizeof(struct link_ea_header);
94         lee = (struct link_ea_entry *)(leh + 1);
95         printf("%s: count %u\n", fname, leh->leh_reccount);
96         for (i = 0; i < leh->leh_reccount; i++) {
97                 reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
98                 length += reclen;
99                 if (length > leh->leh_len) {
100                         fprintf(stderr,
101                                 "%s: length exceeded, expected %llu, got %llu\n",
102                                 fname, (unsigned long long)leh->leh_len,
103                                 (unsigned long long)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 %llu, got %llu\n",
117                         fname,
118                         (unsigned long long)leh->leh_len,
119                         (unsigned long long)length);
120                 return -1;
121         }
122
123         return 0;
124 }
125
126 int main(int argc, char *argv[])
127 {
128         int     rc = 0;
129         int     rc2;
130         int     i;
131
132         for (i = 1; i < argc; i++) {
133                 rc2 = decode_linkea(argv[i]);
134                 if (rc2 != 0)
135                         rc = rc2;
136         }
137
138         return rc;
139 }