Pull bugzilla-5737 into release branch
[linux-2.6] / fs / 9p / fcprint.c
1 /*
2  *  linux/fs/9p/fcprint.c
3  *
4  *  Print 9P call.
5  *
6  *  Copyright (C) 2005 by Latchesar Ionkov <lucho@ionkov.net>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2
10  *  as published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to:
19  *  Free Software Foundation
20  *  51 Franklin Street, Fifth Floor
21  *  Boston, MA  02111-1301  USA
22  *
23  */
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/errno.h>
27 #include <linux/fs.h>
28 #include <linux/idr.h>
29
30 #include "debug.h"
31 #include "v9fs.h"
32 #include "9p.h"
33 #include "mux.h"
34
35 static int
36 v9fs_printqid(char *buf, int buflen, struct v9fs_qid *q)
37 {
38         int n;
39         char b[10];
40
41         n = 0;
42         if (q->type & V9FS_QTDIR)
43                 b[n++] = 'd';
44         if (q->type & V9FS_QTAPPEND)
45                 b[n++] = 'a';
46         if (q->type & V9FS_QTAUTH)
47                 b[n++] = 'A';
48         if (q->type & V9FS_QTEXCL)
49                 b[n++] = 'l';
50         if (q->type & V9FS_QTTMP)
51                 b[n++] = 't';
52         if (q->type & V9FS_QTSYMLINK)
53                 b[n++] = 'L';
54         b[n] = '\0';
55
56         return scnprintf(buf, buflen, "(%.16llx %x %s)", (long long int) q->path,
57                 q->version, b);
58 }
59
60 static int
61 v9fs_printperm(char *buf, int buflen, int perm)
62 {
63         int n;
64         char b[15];
65
66         n = 0;
67         if (perm & V9FS_DMDIR)
68                 b[n++] = 'd';
69         if (perm & V9FS_DMAPPEND)
70                 b[n++] = 'a';
71         if (perm & V9FS_DMAUTH)
72                 b[n++] = 'A';
73         if (perm & V9FS_DMEXCL)
74                 b[n++] = 'l';
75         if (perm & V9FS_DMTMP)
76                 b[n++] = 't';
77         if (perm & V9FS_DMDEVICE)
78                 b[n++] = 'D';
79         if (perm & V9FS_DMSOCKET)
80                 b[n++] = 'S';
81         if (perm & V9FS_DMNAMEDPIPE)
82                 b[n++] = 'P';
83         if (perm & V9FS_DMSYMLINK)
84                 b[n++] = 'L';
85         b[n] = '\0';
86
87         return scnprintf(buf, buflen, "%s%03o", b, perm&077);
88 }
89
90 static int
91 v9fs_printstat(char *buf, int buflen, struct v9fs_stat *st, int extended)
92 {
93         int n;
94
95         n = scnprintf(buf, buflen, "'%.*s' '%.*s'", st->name.len,
96                 st->name.str, st->uid.len, st->uid.str);
97         if (extended)
98                 n += scnprintf(buf+n, buflen-n, "(%d)", st->n_uid);
99
100         n += scnprintf(buf+n, buflen-n, " '%.*s'", st->gid.len, st->gid.str);
101         if (extended)
102                 n += scnprintf(buf+n, buflen-n, "(%d)", st->n_gid);
103
104         n += scnprintf(buf+n, buflen-n, " '%.*s'", st->muid.len, st->muid.str);
105         if (extended)
106                 n += scnprintf(buf+n, buflen-n, "(%d)", st->n_muid);
107
108         n += scnprintf(buf+n, buflen-n, " q ");
109         n += v9fs_printqid(buf+n, buflen-n, &st->qid);
110         n += scnprintf(buf+n, buflen-n, " m ");
111         n += v9fs_printperm(buf+n, buflen-n, st->mode);
112         n += scnprintf(buf+n, buflen-n, " at %d mt %d l %lld",
113                 st->atime, st->mtime, (long long int) st->length);
114
115         if (extended)
116                 n += scnprintf(buf+n, buflen-n, " ext '%.*s'",
117                         st->extension.len, st->extension.str);
118
119         return n;
120 }
121
122 static int
123 v9fs_dumpdata(char *buf, int buflen, u8 *data, int datalen)
124 {
125         int i, n;
126
127         i = n = 0;
128         while (i < datalen) {
129                 n += scnprintf(buf + n, buflen - n, "%02x", data[i]);
130                 if (i%4 == 3)
131                         n += scnprintf(buf + n, buflen - n, " ");
132                 if (i%32 == 31)
133                         n += scnprintf(buf + n, buflen - n, "\n");
134
135                 i++;
136         }
137         n += scnprintf(buf + n, buflen - n, "\n");
138
139         return n;
140 }
141
142 static int
143 v9fs_printdata(char *buf, int buflen, u8 *data, int datalen)
144 {
145         return v9fs_dumpdata(buf, buflen, data, datalen<16?datalen:16);
146 }
147
148 int
149 v9fs_printfcall(char *buf, int buflen, struct v9fs_fcall *fc, int extended)
150 {
151         int i, ret, type, tag;
152
153         if (!fc)
154                 return scnprintf(buf, buflen, "<NULL>");
155
156         type = fc->id;
157         tag = fc->tag;
158
159         ret = 0;
160         switch (type) {
161         case TVERSION:
162                 ret += scnprintf(buf+ret, buflen-ret,
163                         "Tversion tag %u msize %u version '%.*s'", tag,
164                         fc->params.tversion.msize, fc->params.tversion.version.len,
165                         fc->params.tversion.version.str);
166                 break;
167
168         case RVERSION:
169                 ret += scnprintf(buf+ret, buflen-ret,
170                         "Rversion tag %u msize %u version '%.*s'", tag,
171                         fc->params.rversion.msize, fc->params.rversion.version.len,
172                         fc->params.rversion.version.str);
173                 break;
174
175         case TAUTH:
176                 ret += scnprintf(buf+ret, buflen-ret,
177                         "Tauth tag %u afid %d uname '%.*s' aname '%.*s'", tag,
178                         fc->params.tauth.afid, fc->params.tauth.uname.len,
179                         fc->params.tauth.uname.str, fc->params.tauth.aname.len,
180                         fc->params.tauth.aname.str);
181                 break;
182
183         case RAUTH:
184                 ret += scnprintf(buf+ret, buflen-ret, "Rauth tag %u qid ", tag);
185                 v9fs_printqid(buf+ret, buflen-ret, &fc->params.rauth.qid);
186                 break;
187
188         case TATTACH:
189                 ret += scnprintf(buf+ret, buflen-ret,
190                         "Tattach tag %u fid %d afid %d uname '%.*s' aname '%.*s'",
191                         tag, fc->params.tattach.fid, fc->params.tattach.afid,
192                         fc->params.tattach.uname.len, fc->params.tattach.uname.str,
193                         fc->params.tattach.aname.len, fc->params.tattach.aname.str);
194                 break;
195
196         case RATTACH:
197                 ret += scnprintf(buf+ret, buflen-ret, "Rattach tag %u qid ", tag);
198                 v9fs_printqid(buf+ret, buflen-ret, &fc->params.rattach.qid);
199                 break;
200
201         case RERROR:
202                 ret += scnprintf(buf+ret, buflen-ret, "Rerror tag %u ename '%.*s'",
203                         tag, fc->params.rerror.error.len,
204                         fc->params.rerror.error.str);
205                 if (extended)
206                         ret += scnprintf(buf+ret, buflen-ret, " ecode %d\n",
207                                 fc->params.rerror.errno);
208                 break;
209
210         case TFLUSH:
211                 ret += scnprintf(buf+ret, buflen-ret, "Tflush tag %u oldtag %u",
212                         tag, fc->params.tflush.oldtag);
213                 break;
214
215         case RFLUSH:
216                 ret += scnprintf(buf+ret, buflen-ret, "Rflush tag %u", tag);
217                 break;
218
219         case TWALK:
220                 ret += scnprintf(buf+ret, buflen-ret,
221                         "Twalk tag %u fid %d newfid %d nwname %d", tag,
222                         fc->params.twalk.fid, fc->params.twalk.newfid,
223                         fc->params.twalk.nwname);
224                 for(i = 0; i < fc->params.twalk.nwname; i++)
225                         ret += scnprintf(buf+ret, buflen-ret," '%.*s'",
226                                 fc->params.twalk.wnames[i].len,
227                                 fc->params.twalk.wnames[i].str);
228                 break;
229
230         case RWALK:
231                 ret += scnprintf(buf+ret, buflen-ret, "Rwalk tag %u nwqid %d",
232                         tag, fc->params.rwalk.nwqid);
233                 for(i = 0; i < fc->params.rwalk.nwqid; i++)
234                         ret += v9fs_printqid(buf+ret, buflen-ret,
235                                 &fc->params.rwalk.wqids[i]);
236                 break;
237
238         case TOPEN:
239                 ret += scnprintf(buf+ret, buflen-ret,
240                         "Topen tag %u fid %d mode %d", tag,
241                         fc->params.topen.fid, fc->params.topen.mode);
242                 break;
243
244         case ROPEN:
245                 ret += scnprintf(buf+ret, buflen-ret, "Ropen tag %u", tag);
246                 ret += v9fs_printqid(buf+ret, buflen-ret, &fc->params.ropen.qid);
247                 ret += scnprintf(buf+ret, buflen-ret," iounit %d",
248                         fc->params.ropen.iounit);
249                 break;
250
251         case TCREATE:
252                 ret += scnprintf(buf+ret, buflen-ret,
253                         "Tcreate tag %u fid %d name '%.*s' perm ", tag,
254                         fc->params.tcreate.fid, fc->params.tcreate.name.len,
255                         fc->params.tcreate.name.str);
256
257                 ret += v9fs_printperm(buf+ret, buflen-ret, fc->params.tcreate.perm);
258                 ret += scnprintf(buf+ret, buflen-ret, " mode %d",
259                         fc->params.tcreate.mode);
260                 break;
261
262         case RCREATE:
263                 ret += scnprintf(buf+ret, buflen-ret, "Rcreate tag %u", tag);
264                 ret += v9fs_printqid(buf+ret, buflen-ret, &fc->params.rcreate.qid);
265                 ret += scnprintf(buf+ret, buflen-ret, " iounit %d",
266                         fc->params.rcreate.iounit);
267                 break;
268
269         case TREAD:
270                 ret += scnprintf(buf+ret, buflen-ret,
271                         "Tread tag %u fid %d offset %lld count %u", tag,
272                         fc->params.tread.fid,
273                         (long long int) fc->params.tread.offset,
274                         fc->params.tread.count);
275                 break;
276
277         case RREAD:
278                 ret += scnprintf(buf+ret, buflen-ret,
279                         "Rread tag %u count %u data ", tag,
280                         fc->params.rread.count);
281                 ret += v9fs_printdata(buf+ret, buflen-ret, fc->params.rread.data,
282                         fc->params.rread.count);
283                 break;
284
285         case TWRITE:
286                 ret += scnprintf(buf+ret, buflen-ret,
287                         "Twrite tag %u fid %d offset %lld count %u data ",
288                         tag, fc->params.twrite.fid,
289                         (long long int) fc->params.twrite.offset,
290                         fc->params.twrite.count);
291                 ret += v9fs_printdata(buf+ret, buflen-ret, fc->params.twrite.data,
292                         fc->params.twrite.count);
293                 break;
294
295         case RWRITE:
296                 ret += scnprintf(buf+ret, buflen-ret, "Rwrite tag %u count %u",
297                         tag, fc->params.rwrite.count);
298                 break;
299
300         case TCLUNK:
301                 ret += scnprintf(buf+ret, buflen-ret, "Tclunk tag %u fid %d",
302                         tag, fc->params.tclunk.fid);
303                 break;
304
305         case RCLUNK:
306                 ret += scnprintf(buf+ret, buflen-ret, "Rclunk tag %u", tag);
307                 break;
308
309         case TREMOVE:
310                 ret += scnprintf(buf+ret, buflen-ret, "Tremove tag %u fid %d",
311                         tag, fc->params.tremove.fid);
312                 break;
313
314         case RREMOVE:
315                 ret += scnprintf(buf+ret, buflen-ret, "Rremove tag %u", tag);
316                 break;
317
318         case TSTAT:
319                 ret += scnprintf(buf+ret, buflen-ret, "Tstat tag %u fid %d",
320                         tag, fc->params.tstat.fid);
321                 break;
322
323         case RSTAT:
324                 ret += scnprintf(buf+ret, buflen-ret, "Rstat tag %u ", tag);
325                 ret += v9fs_printstat(buf+ret, buflen-ret, &fc->params.rstat.stat,
326                         extended);
327                 break;
328
329         case TWSTAT:
330                 ret += scnprintf(buf+ret, buflen-ret, "Twstat tag %u fid %d ",
331                         tag, fc->params.twstat.fid);
332                 ret += v9fs_printstat(buf+ret, buflen-ret, &fc->params.twstat.stat,
333                         extended);
334                 break;
335
336         case RWSTAT:
337                 ret += scnprintf(buf+ret, buflen-ret, "Rwstat tag %u", tag);
338                 break;
339
340         default:
341                 ret += scnprintf(buf+ret, buflen-ret, "unknown type %d", type);
342                 break;
343         }
344
345         return ret;
346 }