Whamcloud - gitweb
b=14184
[fs/lustre-release.git] / lustre / mds / lproc_mds.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
9  *   You may have signed or agreed to another license before downloading
10  *   this software.  If so, you are bound by the terms and conditions
11  *   of that agreement, and the following does not apply to you.  See the
12  *   LICENSE file included with this distribution for more information.
13  *
14  *   If you did not agree to a different license, then this copy of Lustre
15  *   is open source software; you can redistribute it and/or modify it
16  *   under the terms of version 2 of the GNU General Public License as
17  *   published by the Free Software Foundation.
18  *
19  *   In either case, Lustre is distributed in the hope that it will be
20  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   license text for more details.
23  *
24  */
25 #define DEBUG_SUBSYSTEM S_CLASS
26
27 #include <linux/version.h>
28 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
29 #include <asm/statfs.h>
30 #endif
31 #include <obd.h>
32 #include <obd_class.h>
33 #include <lprocfs_status.h>
34 #include "mds_internal.h"
35
36 #ifdef LPROCFS
37 static int lprocfs_mds_rd_mntdev(char *page, char **start, off_t off, int count,
38                                  int *eof, void *data)
39 {
40         struct obd_device* obd = (struct obd_device *)data;
41
42         LASSERT(obd != NULL);
43         LASSERT(obd->u.mds.mds_vfsmnt->mnt_devname);
44         *eof = 1;
45
46         return snprintf(page, count, "%s\n",obd->u.mds.mds_vfsmnt->mnt_devname);
47 }
48
49 static int lprocfs_mds_rd_evictostnids(char *page, char **start, off_t off,
50                                        int count, int *eof, void *data)
51 {
52         struct obd_device* obd = (struct obd_device *)data;
53
54         LASSERT(obd != NULL);
55
56         return snprintf(page, count, "%d\n", obd->u.mds.mds_evict_ost_nids);
57 }
58
59 static int lprocfs_mds_wr_evictostnids(struct file *file, const char *buffer,
60                                        unsigned long count, void *data)
61 {
62         struct obd_device *obd = data;
63         int val, rc;
64
65         rc = lprocfs_write_helper(buffer, count, &val);
66         if (rc)
67                 return rc;
68
69         obd->u.mds.mds_evict_ost_nids = !!val;
70
71         return count;
72 }
73
74 static int lprocfs_mds_wr_evict_client(struct file *file, const char *buffer,
75                                        unsigned long count, void *data)
76 {
77         struct obd_device *obd = data;
78         struct mds_obd *mds = &obd->u.mds;
79         char tmpbuf[sizeof(struct obd_uuid)];
80         struct ptlrpc_request_set *set;
81         int rc;
82
83         sscanf(buffer, "%40s", tmpbuf);
84
85         if (strncmp(tmpbuf, "nid:", 4) != 0)
86                 return lprocfs_wr_evict_client(file, buffer, count, data);
87
88         set = ptlrpc_prep_set();
89         if (!set)
90                 return -ENOMEM;
91
92         if (obd->u.mds.mds_evict_ost_nids) {
93                 rc = obd_set_info_async(mds->mds_osc_exp,strlen("evict_by_nid"),
94                                         "evict_by_nid", strlen(tmpbuf + 4) + 1,
95                                         tmpbuf + 4, set);
96                 if (rc)
97                         CERROR("Failed to evict nid %s from OSTs: rc %d\n",
98                                tmpbuf + 4, rc);
99                 ptlrpc_check_set(set);
100         }
101
102         /* See the comments in function lprocfs_wr_evict_client() 
103          * in ptlrpc/lproc_ptlrpc.c for details. - jay */
104         class_incref(obd);
105         LPROCFS_EXIT();
106
107         obd_export_evict_by_nid(obd, tmpbuf+4);
108
109         LPROCFS_ENTRY();
110         class_decref(obd);
111
112         rc = ptlrpc_set_wait(set);
113         if (rc)
114                 CERROR("Failed to evict nid %s from OSTs: rc %d\n", tmpbuf + 4,
115                        rc);
116         ptlrpc_set_destroy(set);
117         return count;
118 }
119
120 static int lprocfs_wr_group_info(struct file *file, const char *buffer,
121                                  unsigned long count, void *data)
122 {
123         struct obd_device *obd = data;
124         struct mds_obd *mds = &obd->u.mds;
125         struct mds_grp_downcall_data sparam, *param = &sparam;
126         int size = 0, rc = count;
127
128         if (count < sizeof(param)) {
129                 CERROR("%s: invalid data size %lu\n", obd->obd_name, count);
130                 return count;
131         }
132
133         if (copy_from_user(param, buffer, sizeof(*param)) ||
134             param->mgd_magic != MDS_GRP_DOWNCALL_MAGIC) {
135                 CERROR("%s: MDS group downcall bad params\n", obd->obd_name);
136                 return count;
137         }
138
139         if (param->mgd_ngroups > NGROUPS_MAX) {
140                 CWARN("%s: uid %u groups %d more than maximum %d\n",
141                       obd->obd_name, param->mgd_uid, param->mgd_ngroups,
142                       NGROUPS_MAX);
143                 param->mgd_ngroups = NGROUPS_MAX;
144         }
145
146         if (param->mgd_ngroups > 0) {
147                 size = offsetof(struct mds_grp_downcall_data,
148                                 mgd_groups[param->mgd_ngroups]);
149                 OBD_ALLOC(param, size);
150                 if (!param) {
151                         CERROR("%s: fail to alloc %d bytes for uid %u"
152                                " with %d groups\n", obd->obd_name, size,
153                                sparam.mgd_uid, sparam.mgd_ngroups);
154                         param = &sparam;
155                         param->mgd_ngroups = 0;
156                 } else if (copy_from_user(param, buffer, size)) {
157                         CERROR("%s: uid %u bad supplementary group data\n",
158                                obd->obd_name, sparam.mgd_uid);
159                         OBD_FREE(param, size);
160                         param = &sparam;
161                         param->mgd_ngroups = 0;
162                 }
163         }
164         rc = upcall_cache_downcall(mds->mds_group_hash, param->mgd_err,
165                                    param->mgd_uid, param->mgd_gid,
166                                    param->mgd_ngroups, param->mgd_groups);
167
168         if (param && param != &sparam)
169                 OBD_FREE(param, size);
170
171         return rc;
172 }
173
174 static int lprocfs_rd_group_expire(char *page, char **start, off_t off,
175                                    int count, int *eof, void *data)
176 {
177         struct obd_device *obd = data;
178
179         *eof = 1;
180         return snprintf(page, count, "%lu\n",
181                         obd->u.mds.mds_group_hash->uc_entry_expire / HZ);
182 }
183
184 static int lprocfs_wr_group_expire(struct file *file, const char *buffer,
185                                    unsigned long count, void *data)
186 {
187         struct obd_device *obd = data;
188         int val, rc;
189
190         rc = lprocfs_write_helper(buffer, count, &val);
191         if (rc)
192                 return rc;
193
194         if (val > 5)
195                 obd->u.mds.mds_group_hash->uc_entry_expire = val * HZ;
196         else
197                 CERROR("invalid expire time %u for group cache\n", val);
198
199         return count;
200 }
201
202 static int lprocfs_rd_group_acquire_expire(char *page, char **start, off_t off,
203                                            int count, int *eof, void *data)
204 {
205         struct obd_device *obd = data;
206
207         *eof = 1;
208         return snprintf(page, count, "%lu\n",
209                         obd->u.mds.mds_group_hash->uc_acquire_expire / HZ);
210 }
211
212 static int lprocfs_wr_group_acquire_expire(struct file *file,const char *buffer,
213                                            unsigned long count, void *data)
214 {
215         struct obd_device *obd = data;
216         int val, rc = 0;
217
218         rc = lprocfs_write_helper(buffer, count, &val);
219         if (rc)
220                 return rc;
221
222         if (val > 2)
223                 obd->u.mds.mds_group_hash->uc_acquire_expire = val * HZ;
224
225         return count;
226 }
227
228 static int lprocfs_rd_group_upcall(char *page, char **start, off_t off,
229                                    int count, int *eof, void *data)
230 {
231         struct obd_device *obd = data;
232
233         *eof = 1;
234         return snprintf(page, count, "%s\n",
235                         obd->u.mds.mds_group_hash->uc_upcall);
236 }
237
238 static int lprocfs_wr_group_upcall(struct file *file, const char *buffer,
239                                    unsigned long count, void *data)
240 {
241         struct obd_device *obd = data;
242         struct upcall_cache *hash = obd->u.mds.mds_group_hash;
243         char kernbuf[UC_CACHE_UPCALL_MAXPATH] = { '\0' };
244
245         if (count >= UC_CACHE_UPCALL_MAXPATH) {
246                 CERROR("%s: group upcall too long\n", obd->obd_name);
247                 return -EINVAL;
248         }
249
250         if (copy_from_user(kernbuf, buffer,
251                            min(count, UC_CACHE_UPCALL_MAXPATH - 1)))
252                 return -EFAULT;
253
254         /* Remove any extraneous bits from the upcall (e.g. linefeeds) */
255         sscanf(kernbuf, "%s", hash->uc_upcall);
256
257         if (strcmp(hash->uc_name, obd->obd_name) != 0)
258                 CWARN("%s: write to upcall name %s for MDS %s\n",
259                       obd->obd_name, hash->uc_upcall, obd->obd_name);
260         CWARN("%s: group upcall set to %s\n", obd->obd_name, hash->uc_upcall);
261
262         return count;
263 }
264
265 static int lprocfs_wr_group_flush(struct file *file, const char *buffer,
266                                   unsigned long count, void *data)
267 {
268         struct obd_device *obd = data;
269
270         upcall_cache_flush_idle(obd->u.mds.mds_group_hash);
271         return count;
272 }
273
274 static int lprocfs_wr_atime_diff(struct file *file, const char *buffer,
275                                  unsigned long count, void *data)
276 {
277         struct obd_device *obd = data;
278         struct mds_obd *mds = &obd->u.mds;
279         char kernbuf[20], *end;
280         unsigned long diff = 0;
281
282         if (count > (sizeof(kernbuf) - 1))
283                 return -EINVAL;
284
285         if (copy_from_user(kernbuf, buffer, count))
286                 return -EFAULT;
287
288         kernbuf[count] = '\0';
289
290         diff = simple_strtoul(kernbuf, &end, 0);
291         if (kernbuf == end)
292                 return -EINVAL;
293
294         mds->mds_atime_diff = diff;
295         return count;
296 }
297
298 static int lprocfs_rd_atime_diff(char *page, char **start, off_t off,
299                                  int count, int *eof, void *data)
300 {
301         struct obd_device *obd = data;
302         struct mds_obd *mds = &obd->u.mds;
303
304         *eof = 1;
305         return snprintf(page, count, "%lu\n", mds->mds_atime_diff);
306 }
307
308 static int lprocfs_wr_rootsquash(struct file *file, const char *buffer,
309                                  unsigned long count, void *data)
310 {
311         struct obd_device *obd = data;
312         struct mds_obd *mds = &obd->u.mds;
313         char kernbuf[50], *tmp, *end;
314         unsigned long uid, gid;
315
316         if (count > (sizeof(kernbuf) - 1))
317                 return -EINVAL;
318
319         if (copy_from_user(kernbuf, buffer, count))
320                 return -EFAULT;
321
322         kernbuf[count] = '\0';
323
324         uid = simple_strtoul(kernbuf, &tmp, 0);
325         if (kernbuf == tmp) {
326                 if (tmp[0] != ':')
327                         return -EINVAL;
328                 uid = mds->mds_squash_uid;
329         }
330         /* skip ':' */
331         tmp++;
332         gid = simple_strtoul(tmp, &end, 0);
333         if (tmp == end)
334                 gid = mds->mds_squash_gid;
335
336         mds->mds_squash_uid = uid;
337         mds->mds_squash_gid = gid;
338         return count;
339 }
340
341 static int lprocfs_rd_rootsquash(char *page, char **start, off_t off,
342                                  int count, int *eof, void *data)
343 {
344         struct obd_device *obd = data;
345         struct mds_obd *mds = &obd->u.mds;
346
347         *eof = 1;
348         return snprintf(page, count, "%lu:%lu\n",
349                         (unsigned long)mds->mds_squash_uid,
350                         (unsigned long)mds->mds_squash_gid);
351 }
352
353 static int lprocfs_wr_nosquash_nid(struct file *file, const char *buffer,
354                                    unsigned long count, void *data)
355 {
356         struct obd_device *obd = data;
357         struct mds_obd *mds = &obd->u.mds;
358         char kernbuf[30], *start, *end;
359
360         if (count > (sizeof(kernbuf) - 1))
361                 return -EINVAL;
362
363         if (copy_from_user(kernbuf, buffer, count))
364                 return -EFAULT;
365         kernbuf[count] = '\0';
366
367         /* strip frontal whitespaces */
368         start = kernbuf;
369         while (*start && isspace(*start))
370                 start++;
371         /* EOL - string doesn't contain NID */
372         if (*start == '\0')
373                 return -EINVAL;
374         /* strip backward whitespaces */
375         end = kernbuf + count - 1;
376         while (*end && isspace(*end))
377                 end--;
378         *(end + 1) = '\0';
379
380         mds->mds_nosquash_nid = libcfs_str2nid(start);
381         return count;
382 }
383
384 static int lprocfs_rd_nosquash_nid(char *page, char **start, off_t off,
385                                    int count, int *eof, void *data)
386 {
387         struct obd_device *obd = data;
388         struct mds_obd *mds = &obd->u.mds;
389
390         *eof = 1;
391         return snprintf(page, count, "%s\n",
392                         libcfs_nid2str(mds->mds_nosquash_nid));
393 }
394
395 struct lprocfs_vars lprocfs_mds_obd_vars[] = {
396         { "uuid",            lprocfs_rd_uuid,        0, 0 },
397         { "blocksize",       lprocfs_rd_blksize,     0, 0 },
398         { "kbytestotal",     lprocfs_rd_kbytestotal, 0, 0 },
399         { "kbytesfree",      lprocfs_rd_kbytesfree,  0, 0 },
400         { "kbytesavail",     lprocfs_rd_kbytesavail, 0, 0 },
401         { "filestotal",      lprocfs_rd_filestotal,  0, 0 },
402         { "filesfree",       lprocfs_rd_filesfree,   0, 0 },
403         { "fstype",          lprocfs_rd_fstype,      0, 0 },
404         { "mntdev",          lprocfs_mds_rd_mntdev,  0, 0 },
405         { "recovery_status", lprocfs_obd_rd_recovery_status, 0, 0 },
406         { "evict_client",    0,                lprocfs_mds_wr_evict_client, 0 },
407         { "evict_ost_nids",  lprocfs_mds_rd_evictostnids,
408                                                lprocfs_mds_wr_evictostnids, 0 },
409         { "num_exports",     lprocfs_rd_num_exports, 0, 0 },
410 #ifdef HAVE_QUOTA_SUPPORT
411         { "quota_bunit_sz",  lprocfs_rd_bunit, lprocfs_wr_bunit, 0 },
412         { "quota_btune_sz",  lprocfs_rd_btune, lprocfs_wr_btune, 0 },
413         { "quota_iunit_sz",  lprocfs_rd_iunit, lprocfs_wr_iunit, 0 },
414         { "quota_itune_sz",  lprocfs_rd_itune, lprocfs_wr_itune, 0 },
415         { "quota_type",      lprocfs_rd_type, lprocfs_wr_type, 0 },
416 #endif
417         { "group_expire_interval", lprocfs_rd_group_expire,
418                              lprocfs_wr_group_expire, 0},
419         { "group_acquire_expire", lprocfs_rd_group_acquire_expire,
420                              lprocfs_wr_group_acquire_expire, 0},
421         { "group_upcall",    lprocfs_rd_group_upcall,
422                              lprocfs_wr_group_upcall, 0},
423         { "group_flush",     0, lprocfs_wr_group_flush, 0},
424         { "group_info",      0, lprocfs_wr_group_info, 0 },
425         { "atime_diff",      lprocfs_rd_atime_diff, lprocfs_wr_atime_diff, 0 },
426         { "rootsquash",      lprocfs_rd_rootsquash,
427                              lprocfs_wr_rootsquash, 0 },
428         { "nosquash_nid",    lprocfs_rd_nosquash_nid,
429                              lprocfs_wr_nosquash_nid, 0 },
430         { 0 }
431 };
432
433 struct lprocfs_vars lprocfs_mds_module_vars[] = {
434         { "num_refs",     lprocfs_rd_numrefs,     0, 0 },
435         { 0 }
436 };
437
438 struct lprocfs_vars lprocfs_mdt_obd_vars[] = {
439         { "uuid",         lprocfs_rd_uuid,        0, 0 },
440         { 0 }
441 };
442
443 struct lprocfs_vars lprocfs_mdt_module_vars[] = {
444         { "num_refs",     lprocfs_rd_numrefs,     0, 0 },
445         { 0 }
446 };
447
448 void mds_counter_incr(struct obd_export *exp, int opcode)
449 {
450         if (exp->exp_obd && exp->exp_obd->obd_stats)
451                 lprocfs_counter_incr(exp->exp_obd->obd_stats, opcode);
452         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats != NULL)
453                 lprocfs_counter_incr(exp->exp_nid_stats->nid_stats, opcode);
454
455 }
456
457 void mds_stats_counter_init(struct lprocfs_stats *stats)
458 {
459         lprocfs_counter_init(stats, LPROC_MDS_OPEN, 0, "open", "reqs");
460         lprocfs_counter_init(stats, LPROC_MDS_CLOSE, 0, "close", "reqs");
461         lprocfs_counter_init(stats, LPROC_MDS_MKNOD, 0, "mknod", "reqs");
462         lprocfs_counter_init(stats, LPROC_MDS_LINK, 0, "link", "reqs");
463         lprocfs_counter_init(stats, LPROC_MDS_UNLINK, 0, "unlink", "reqs");
464         lprocfs_counter_init(stats, LPROC_MDS_MKDIR, 0, "mkdir", "reqs");
465         lprocfs_counter_init(stats, LPROC_MDS_RMDIR, 0, "rmdir", "reqs");
466         lprocfs_counter_init(stats, LPROC_MDS_RENAME, 0, "rename", "reqs");
467         lprocfs_counter_init(stats, LPROC_MDS_GETXATTR, 0, "getxattr", "reqs");
468         lprocfs_counter_init(stats, LPROC_MDS_SETXATTR, 0, "setxattr", "reqs");
469 }
470
471 void lprocfs_mds_init_vars(struct lprocfs_static_vars *lvars)
472 {
473     lvars->module_vars = lprocfs_mds_module_vars;
474     lvars->obd_vars = lprocfs_mds_obd_vars;
475 }
476
477 void lprocfs_mdt_init_vars(struct lprocfs_static_vars *lvars)
478 {
479     lvars->module_vars = lprocfs_mdt_module_vars;
480     lvars->obd_vars = lprocfs_mdt_obd_vars;
481 }
482 #endif