Whamcloud - gitweb
LU-12330 obdclass: allow per-session jobids.
[fs/lustre-release.git] / lustre / obdclass / jobid.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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2014, Intel Corporation.
27  *
28  * Copyright 2017 Cray Inc, all rights reserved.
29  * Author: Ben Evans.
30  *
31  * Store PID->JobID mappings
32  */
33
34 #define DEBUG_SUBSYSTEM S_RPC
35 #include <linux/user_namespace.h>
36 #ifdef HAVE_UIDGID_HEADER
37 #include <linux/uidgid.h>
38 #endif
39 #include <linux/utsname.h>
40
41 #include <libcfs/libcfs.h>
42 #include <obd_support.h>
43 #include <obd_class.h>
44 #include <lustre_net.h>
45
46 static struct cfs_hash *jobid_hash;
47 static struct cfs_hash_ops jobid_hash_ops;
48 spinlock_t jobid_hash_lock;
49
50 #define RESCAN_INTERVAL 30
51 #define DELETE_INTERVAL 300
52
53 char obd_jobid_var[JOBSTATS_JOBID_VAR_MAX_LEN + 1] = JOBSTATS_DISABLE;
54 char obd_jobid_name[LUSTRE_JOBID_SIZE] = "%e.%u";
55
56 /**
57  * Structure to store a single PID->JobID mapping
58  */
59 struct jobid_pid_map {
60         struct hlist_node       jp_hash;
61         time64_t                jp_time;
62         spinlock_t              jp_lock; /* protects jp_jobid */
63         char                    jp_jobid[LUSTRE_JOBID_SIZE];
64         unsigned int            jp_joblen;
65         atomic_t                jp_refcount;
66         pid_t                   jp_pid;
67 };
68
69 /*
70  * Jobid can be set for a session (see setsid(2)) by writing to
71  * a sysfs file from any process in that session.
72  * The jobids are stored in a hash table indexed by the relevant
73  * struct pid.  We periodically look for entries where the pid has
74  * no PIDTYPE_SID tasks any more, and prune them.  This happens within
75  * 5 seconds of a jobid being added, and every 5 minutes when jobids exist,
76  * but none are added.
77  */
78 #define JOBID_EXPEDITED_CLEAN (5)
79 #define JOBID_BACKGROUND_CLEAN (5 * 60)
80
81 struct session_jobid {
82         struct pid              *sj_session;
83         struct rhash_head       sj_linkage;
84         struct rcu_head         sj_rcu;
85         char                    sj_jobid[1];
86 };
87
88 static const struct rhashtable_params jobid_params = {
89         .key_len        = sizeof(struct pid *),
90         .key_offset     = offsetof(struct session_jobid, sj_session),
91         .head_offset    = offsetof(struct session_jobid, sj_linkage),
92 };
93
94 static struct rhashtable session_jobids;
95
96 /*
97  * jobid_current must be called with rcu_read_lock held.
98  * if it returns non-NULL, the string can only be used
99  * until rcu_read_unlock is called.
100  */
101 char *jobid_current(void)
102 {
103         struct pid *sid = task_session(current);
104         struct session_jobid *sj;
105
106         sj = rhashtable_lookup_fast(&session_jobids, &sid, jobid_params);
107         if (sj)
108                 return sj->sj_jobid;
109         return NULL;
110 }
111
112 static void jobid_prune_expedite(void);
113 /*
114  * jobid_set_current will try to add a new entry
115  * to the table.  If one exists with the same key, the
116  * jobid will be replaced
117  */
118 int jobid_set_current(char *jobid)
119 {
120         struct pid *sid;
121         struct session_jobid *sj, *origsj;
122         int ret;
123         int len = strlen(jobid);
124
125         sj = kmalloc(sizeof(*sj) + len, GFP_KERNEL);
126         if (!sj)
127                 return -ENOMEM;
128         rcu_read_lock();
129         sid = task_session(current);
130         sj->sj_session = get_pid(sid);
131         strncpy(sj->sj_jobid, jobid, len+1);
132         origsj = rhashtable_lookup_get_insert_fast(&session_jobids,
133                                                    &sj->sj_linkage,
134                                                    jobid_params);
135         if (origsj == NULL) {
136                 /* successful insert */
137                 rcu_read_unlock();
138                 jobid_prune_expedite();
139                 return 0;
140         }
141
142         if (IS_ERR(origsj)) {
143                 put_pid(sj->sj_session);
144                 kfree(sj);
145                 rcu_read_unlock();
146                 return PTR_ERR(origsj);
147         }
148         ret = rhashtable_replace_fast(&session_jobids,
149                                       &origsj->sj_linkage,
150                                       &sj->sj_linkage,
151                                       jobid_params);
152         if (ret) {
153                 put_pid(sj->sj_session);
154                 kfree(sj);
155                 rcu_read_unlock();
156                 return ret;
157         }
158         put_pid(origsj->sj_session);
159         rcu_read_unlock();
160         kfree_rcu(origsj, sj_rcu);
161         jobid_prune_expedite();
162
163         return 0;
164 }
165
166 static void jobid_free(void *vsj, void *arg)
167 {
168         struct session_jobid *sj = vsj;
169
170         put_pid(sj->sj_session);
171         kfree(sj);
172 }
173
174 static void jobid_prune(struct work_struct *work);
175 static DECLARE_DELAYED_WORK(jobid_prune_work, jobid_prune);
176 static int jobid_prune_expedited;
177 static void jobid_prune(struct work_struct *work)
178 {
179         int remaining = 0;
180         struct rhashtable_iter iter;
181         struct session_jobid *sj;
182
183         jobid_prune_expedited = 0;
184         rhashtable_walk_enter(&session_jobids, &iter);
185         rhashtable_walk_start(&iter);
186         while ((sj = rhashtable_walk_next(&iter)) != NULL) {
187                 if (!hlist_empty(&sj->sj_session->tasks[PIDTYPE_SID])) {
188                         remaining++;
189                         continue;
190                 }
191                 if (rhashtable_remove_fast(&session_jobids,
192                                            &sj->sj_linkage,
193                                            jobid_params) == 0) {
194                         put_pid(sj->sj_session);
195                         kfree_rcu(sj, sj_rcu);
196                 }
197         }
198         rhashtable_walk_stop(&iter);
199         rhashtable_walk_exit(&iter);
200         if (remaining)
201                 schedule_delayed_work(&jobid_prune_work,
202                                       cfs_time_seconds(JOBID_BACKGROUND_CLEAN));
203 }
204
205 static void jobid_prune_expedite(void)
206 {
207         if (!jobid_prune_expedited) {
208                 jobid_prune_expedited = 1;
209                 mod_delayed_work(system_wq, &jobid_prune_work,
210                                  cfs_time_seconds(JOBID_EXPEDITED_CLEAN));
211         }
212 }
213
214 /*
215  * Get jobid of current process by reading the environment variable
216  * stored in between the "env_start" & "env_end" of task struct.
217  *
218  * If some job scheduler doesn't store jobid in the "env_start/end",
219  * then an upcall could be issued here to get the jobid by utilizing
220  * the userspace tools/API. Then, the jobid must be cached.
221  */
222 int jobid_get_from_environ(char *jobid_var, char *jobid, int *jobid_len)
223 {
224         static bool printed;
225         int rc;
226
227         rc = cfs_get_environ(jobid_var, jobid, jobid_len);
228         if (!rc)
229                 goto out;
230
231         if (unlikely(rc == -EOVERFLOW && !printed)) {
232                 /* For the PBS_JOBID and LOADL_STEP_ID keys (which are
233                  * variable length strings instead of just numbers), it
234                  * might make sense to keep the unique parts for JobID,
235                  * instead of just returning an error.  That means a
236                  * larger temp buffer for cfs_get_environ(), then
237                  * truncating the string at some separator to fit into
238                  * the specified jobid_len.  Fix later if needed. */
239                 LCONSOLE_WARN("jobid: '%s' value too large (%d)\n",
240                               obd_jobid_var, *jobid_len);
241                 printed = true;
242                 rc = 0;
243         }
244         if (rc) {
245                 CDEBUG((rc == -ENOENT || rc == -EINVAL ||
246                         rc == -EDEADLK) ? D_INFO : D_ERROR,
247                        "jobid: get '%s' failed: rc = %d\n",
248                        obd_jobid_var, rc);
249         }
250
251 out:
252         return rc;
253 }
254
255 /*
256  * jobid_should_free_item
257  *
258  * Each item is checked to see if it should be released
259  * Removed from hash table by caller
260  * Actually freed in jobid_put_locked
261  *
262  * Returns 1 if item is to be freed, 0 if it is to be kept
263  */
264
265 static int jobid_should_free_item(void *obj, void *data)
266 {
267         char *jobid = data;
268         struct jobid_pid_map *pidmap = obj;
269         int rc = 0;
270
271         if (obj == NULL)
272                 return 0;
273
274         if (jobid == NULL) {
275                 WARN_ON_ONCE(atomic_read(&pidmap->jp_refcount) != 1);
276                 return 1;
277         }
278
279         spin_lock(&pidmap->jp_lock);
280         /* prevent newly inserted items from deleting */
281         if (jobid[0] == '\0' && atomic_read(&pidmap->jp_refcount) == 1)
282                 rc = 1;
283         else if (ktime_get_real_seconds() - pidmap->jp_time > DELETE_INTERVAL)
284                 rc = 1;
285         else if (strcmp(pidmap->jp_jobid, jobid) == 0)
286                 rc = 1;
287         spin_unlock(&pidmap->jp_lock);
288
289         return rc;
290 }
291
292 /*
293  * jobid_name_is_valid
294  *
295  * Checks if the jobid is a Lustre process
296  *
297  * Returns true if jobid is valid
298  * Returns false if jobid looks like it's a Lustre process
299  */
300 static bool jobid_name_is_valid(char *jobid)
301 {
302         const char *const lustre_reserved[] = { "ll_ping", "ptlrpc",
303                                                 "ldlm", "ll_sa", NULL };
304         int i;
305
306         if (jobid[0] == '\0')
307                 return false;
308
309         for (i = 0; lustre_reserved[i] != NULL; i++) {
310                 if (strncmp(jobid, lustre_reserved[i],
311                             strlen(lustre_reserved[i])) == 0)
312                         return false;
313         }
314         return true;
315 }
316
317 /*
318  * jobid_get_from_cache()
319  *
320  * Returns contents of jobid_var from process environment for current PID,
321  * or from the per-session jobid table.
322  * Values fetch from process environment will be cached for some time to avoid
323  * the overhead of scanning the environment.
324  *
325  * Return: -ENOMEM if allocating a new pidmap fails
326  *         -ENOENT if no entry could be found
327  *         +ve string length for success (something was returned in jobid)
328  */
329 static int jobid_get_from_cache(char *jobid, size_t joblen)
330 {
331         static time64_t last_expire;
332         bool expire_cache = false;
333         pid_t pid = current_pid();
334         struct jobid_pid_map *pidmap = NULL;
335         time64_t now = ktime_get_real_seconds();
336         int rc = 0;
337         ENTRY;
338
339         if (strcmp(obd_jobid_var, JOBSTATS_SESSION) == 0) {
340                 char *jid;
341
342                 rcu_read_lock();
343                 jid = jobid_current();
344                 if (jid) {
345                         strlcpy(jobid, jid, joblen);
346                         joblen = strlen(jobid);
347                 } else {
348                         rc = -ENOENT;
349                 }
350                 rcu_read_unlock();
351                 GOTO(out, rc);
352         }
353
354         LASSERT(jobid_hash != NULL);
355
356         /* scan hash periodically to remove old PID entries from cache */
357         spin_lock(&jobid_hash_lock);
358         if (unlikely(last_expire + DELETE_INTERVAL <= now)) {
359                 expire_cache = true;
360                 last_expire = now;
361         }
362         spin_unlock(&jobid_hash_lock);
363
364         if (expire_cache)
365                 cfs_hash_cond_del(jobid_hash, jobid_should_free_item,
366                                   "intentionally_bad_jobid");
367
368         /* first try to find PID in the hash and use that value */
369         pidmap = cfs_hash_lookup(jobid_hash, &pid);
370         if (pidmap == NULL) {
371                 struct jobid_pid_map *pidmap2;
372
373                 OBD_ALLOC_PTR(pidmap);
374                 if (pidmap == NULL)
375                         GOTO(out, rc = -ENOMEM);
376
377                 pidmap->jp_pid = pid;
378                 pidmap->jp_time = 0;
379                 pidmap->jp_jobid[0] = '\0';
380                 spin_lock_init(&pidmap->jp_lock);
381                 INIT_HLIST_NODE(&pidmap->jp_hash);
382                 /*
383                  * @pidmap might be reclaimed just after it is added into
384                  * hash list, init @jp_refcount as 1 to make sure memory
385                  * could be not freed during access.
386                  */
387                 atomic_set(&pidmap->jp_refcount, 1);
388
389                 /*
390                  * Add the newly created map to the hash, on key collision we
391                  * lost a racing addition and must destroy our newly allocated
392                  * map.  The object which exists in the hash will be returned.
393                  */
394                 pidmap2 = cfs_hash_findadd_unique(jobid_hash, &pid,
395                                                   &pidmap->jp_hash);
396                 if (unlikely(pidmap != pidmap2)) {
397                         CDEBUG(D_INFO, "jobid: duplicate found for PID=%u\n",
398                                pid);
399                         OBD_FREE_PTR(pidmap);
400                         pidmap = pidmap2;
401                 }
402         }
403
404         /*
405          * If pidmap is old (this is always true for new entries) refresh it.
406          * If obd_jobid_var is not found, cache empty entry and try again
407          * later, to avoid repeat lookups for PID if obd_jobid_var missing.
408          */
409         spin_lock(&pidmap->jp_lock);
410         if (pidmap->jp_time + RESCAN_INTERVAL <= now) {
411                 char env_jobid[LUSTRE_JOBID_SIZE] = "";
412                 int env_len = sizeof(env_jobid);
413
414                 pidmap->jp_time = now;
415
416                 spin_unlock(&pidmap->jp_lock);
417                 rc = jobid_get_from_environ(obd_jobid_var, env_jobid, &env_len);
418
419                 CDEBUG(D_INFO, "jobid: PID mapping established: %d->%s\n",
420                        pidmap->jp_pid, env_jobid);
421                 spin_lock(&pidmap->jp_lock);
422                 if (!rc) {
423                         pidmap->jp_joblen = env_len;
424                         strlcpy(pidmap->jp_jobid, env_jobid,
425                                 sizeof(pidmap->jp_jobid));
426                         rc = 0;
427                 } else if (rc == -ENOENT) {
428                         /* It might have been deleted, clear out old entry */
429                         pidmap->jp_joblen = 0;
430                         pidmap->jp_jobid[0] = '\0';
431                 }
432         }
433
434         /*
435          * Regardless of how pidmap was found, if it contains a valid entry
436          * use that for now.  If there was a technical error (e.g. -ENOMEM)
437          * use the old cached value until it can be looked up again properly.
438          * If a cached missing entry was found, return -ENOENT.
439          */
440         if (pidmap->jp_joblen) {
441                 strlcpy(jobid, pidmap->jp_jobid, joblen);
442                 joblen = pidmap->jp_joblen;
443                 rc = 0;
444         } else if (!rc) {
445                 rc = -ENOENT;
446         }
447         spin_unlock(&pidmap->jp_lock);
448
449         cfs_hash_put(jobid_hash, &pidmap->jp_hash);
450
451         EXIT;
452 out:
453         return rc < 0 ? rc : joblen;
454 }
455
456 /*
457  * jobid_interpret_string()
458  *
459  * Interpret the jobfmt string to expand specified fields, like coredumps do:
460  *   %e = executable
461  *   %g = gid
462  *   %h = hostname
463  *   %j = jobid from environment
464  *   %p = pid
465  *   %u = uid
466  *
467  * Unknown escape strings are dropped.  Other characters are copied through,
468  * excluding whitespace (to avoid making jobid parsing difficult).
469  *
470  * Return: -EOVERFLOW if the expanded string does not fit within @joblen
471  *         0 for success
472  */
473 static int jobid_interpret_string(const char *jobfmt, char *jobid,
474                                   ssize_t joblen)
475 {
476         char c;
477
478         while ((c = *jobfmt++) && joblen > 1) {
479                 char f;
480                 int l;
481
482                 if (isspace(c)) /* Don't allow embedded spaces */
483                         continue;
484
485                 if (c != '%') {
486                         *jobid = c;
487                         joblen--;
488                         jobid++;
489                         continue;
490                 }
491
492                 switch ((f = *jobfmt++)) {
493                 case 'e': /* executable name */
494                         l = snprintf(jobid, joblen, "%s", current_comm());
495                         break;
496                 case 'g': /* group ID */
497                         l = snprintf(jobid, joblen, "%u",
498                                      from_kgid(&init_user_ns, current_fsgid()));
499                         break;
500                 case 'h': /* hostname */
501                         l = snprintf(jobid, joblen, "%s",
502                                      init_utsname()->nodename);
503                         break;
504                 case 'j': /* jobid stored in process environment */
505                         l = jobid_get_from_cache(jobid, joblen);
506                         if (l < 0)
507                                 l = 0;
508                         break;
509                 case 'p': /* process ID */
510                         l = snprintf(jobid, joblen, "%u", current_pid());
511                         break;
512                 case 'u': /* user ID */
513                         l = snprintf(jobid, joblen, "%u",
514                                      from_kuid(&init_user_ns, current_fsuid()));
515                         break;
516                 case '\0': /* '%' at end of format string */
517                         l = 0;
518                         goto out;
519                 default: /* drop unknown %x format strings */
520                         l = 0;
521                         break;
522                 }
523                 jobid += l;
524                 joblen -= l;
525         }
526         /*
527          * This points at the end of the buffer, so long as jobid is always
528          * incremented the same amount as joblen is decremented.
529          */
530 out:
531         jobid[joblen - 1] = '\0';
532
533         return joblen < 0 ? -EOVERFLOW : 0;
534 }
535
536 /*
537  * Hash initialization, copied from server-side job stats bucket sizes
538  */
539 #define HASH_JOBID_BKT_BITS 5
540 #define HASH_JOBID_CUR_BITS 7
541 #define HASH_JOBID_MAX_BITS 12
542
543 int jobid_cache_init(void)
544 {
545         int rc = 0;
546         ENTRY;
547
548         if (jobid_hash)
549                 return 0;
550
551         spin_lock_init(&jobid_hash_lock);
552         jobid_hash = cfs_hash_create("JOBID_HASH", HASH_JOBID_CUR_BITS,
553                                      HASH_JOBID_MAX_BITS, HASH_JOBID_BKT_BITS,
554                                      0, CFS_HASH_MIN_THETA, CFS_HASH_MAX_THETA,
555                                      &jobid_hash_ops, CFS_HASH_DEFAULT);
556         if (!jobid_hash) {
557                 rc = -ENOMEM;
558         } else {
559                 rc = rhashtable_init(&session_jobids, &jobid_params);
560                 if (rc) {
561                         cfs_hash_putref(jobid_hash);
562                         jobid_hash = NULL;
563                 }
564         }
565
566         RETURN(rc);
567 }
568 EXPORT_SYMBOL(jobid_cache_init);
569
570 void jobid_cache_fini(void)
571 {
572         struct cfs_hash *tmp_hash;
573         ENTRY;
574
575         spin_lock(&jobid_hash_lock);
576         tmp_hash = jobid_hash;
577         jobid_hash = NULL;
578         spin_unlock(&jobid_hash_lock);
579
580         cancel_delayed_work_sync(&jobid_prune_work);
581
582         if (tmp_hash != NULL) {
583                 cfs_hash_cond_del(tmp_hash, jobid_should_free_item, NULL);
584                 cfs_hash_putref(tmp_hash);
585
586                 rhashtable_free_and_destroy(&session_jobids, jobid_free, NULL);
587         }
588
589
590         EXIT;
591 }
592 EXPORT_SYMBOL(jobid_cache_fini);
593
594 /*
595  * Hash operations for pid<->jobid
596  */
597 static unsigned jobid_hashfn(struct cfs_hash *hs, const void *key,
598                              unsigned mask)
599 {
600         return cfs_hash_djb2_hash(key, sizeof(pid_t), mask);
601 }
602
603 static void *jobid_key(struct hlist_node *hnode)
604 {
605         struct jobid_pid_map *pidmap;
606
607         pidmap = hlist_entry(hnode, struct jobid_pid_map, jp_hash);
608         return &pidmap->jp_pid;
609 }
610
611 static int jobid_keycmp(const void *key, struct hlist_node *hnode)
612 {
613         const pid_t *pid_key1;
614         const pid_t *pid_key2;
615
616         LASSERT(key != NULL);
617         pid_key1 = (pid_t *)key;
618         pid_key2 = (pid_t *)jobid_key(hnode);
619
620         return *pid_key1 == *pid_key2;
621 }
622
623 static void *jobid_object(struct hlist_node *hnode)
624 {
625         return hlist_entry(hnode, struct jobid_pid_map, jp_hash);
626 }
627
628 static void jobid_get(struct cfs_hash *hs, struct hlist_node *hnode)
629 {
630         struct jobid_pid_map *pidmap;
631
632         pidmap = hlist_entry(hnode, struct jobid_pid_map, jp_hash);
633
634         atomic_inc(&pidmap->jp_refcount);
635 }
636
637 static void jobid_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
638 {
639         struct jobid_pid_map *pidmap;
640
641         if (hnode == NULL)
642                 return;
643
644         pidmap = hlist_entry(hnode, struct jobid_pid_map, jp_hash);
645         LASSERT(atomic_read(&pidmap->jp_refcount) > 0);
646         if (atomic_dec_and_test(&pidmap->jp_refcount)) {
647                 CDEBUG(D_INFO, "Freeing: %d->%s\n",
648                        pidmap->jp_pid, pidmap->jp_jobid);
649
650                 OBD_FREE_PTR(pidmap);
651         }
652 }
653
654 static struct cfs_hash_ops jobid_hash_ops = {
655         .hs_hash        = jobid_hashfn,
656         .hs_keycmp      = jobid_keycmp,
657         .hs_key         = jobid_key,
658         .hs_object      = jobid_object,
659         .hs_get         = jobid_get,
660         .hs_put         = jobid_put_locked,
661         .hs_put_locked  = jobid_put_locked,
662 };
663
664 /**
665  * Generate the job identifier string for this process for tracking purposes.
666  *
667  * Fill in @jobid string based on the value of obd_jobid_var:
668  * JOBSTATS_DISABLE:      none
669  * JOBSTATS_NODELOCAL:    content of obd_jobid_name (jobid_interpret_string())
670  * JOBSTATS_PROCNAME_UID: process name/UID
671  * JOBSTATS_SESSION       per-session value set by
672  *                            /sys/fs/lustre/jobid_this_session
673  * anything else:         look up obd_jobid_var in the processes environment
674  *
675  * Return -ve error number, 0 on success.
676  */
677 int lustre_get_jobid(char *jobid, size_t joblen)
678 {
679         int rc = 0;
680         ENTRY;
681
682         if (unlikely(joblen < 2)) {
683                 if (joblen == 1)
684                         jobid[0] = '\0';
685                 RETURN(-EINVAL);
686         }
687
688         if (strcmp(obd_jobid_var, JOBSTATS_DISABLE) == 0) {
689                 /* Jobstats isn't enabled */
690                 memset(jobid, 0, joblen);
691         } else if (strcmp(obd_jobid_var, JOBSTATS_NODELOCAL) == 0) {
692                 /* Whole node dedicated to single job */
693                 rc = jobid_interpret_string(obd_jobid_name, jobid, joblen);
694         } else if (strcmp(obd_jobid_var, JOBSTATS_PROCNAME_UID) == 0) {
695                 rc = jobid_interpret_string("%e.%u", jobid, joblen);
696         } else if (strcmp(obd_jobid_var, JOBSTATS_SESSION) == 0) {
697                 char *jid;
698
699                 rcu_read_lock();
700                 jid = jobid_current();
701                 if (jid)
702                         strlcpy(jobid, jid, sizeof(jobid));
703                 rcu_read_unlock();
704         } else if (jobid_name_is_valid(current_comm())) {
705                 /*
706                  * obd_jobid_var holds the jobid environment variable name.
707                  * Skip initial check if obd_jobid_name already uses "%j",
708                  * otherwise try just "%j" first, then fall back to whatever
709                  * is in obd_jobid_name if obd_jobid_var is not found.
710                  */
711                 rc = -EAGAIN;
712                 if (!strnstr(obd_jobid_name, "%j", joblen))
713                         rc = jobid_get_from_cache(jobid, joblen);
714
715                 /* fall back to jobid_node if jobid_var not in environment */
716                 if (rc < 0) {
717                         int rc2 = jobid_interpret_string(obd_jobid_name,
718                                                          jobid, joblen);
719                         if (!rc2)
720                                 rc = 0;
721                 }
722         }
723
724         RETURN(rc);
725 }
726 EXPORT_SYMBOL(lustre_get_jobid);
727
728 /*
729  * lustre_jobid_clear
730  *
731  * Search cache for JobID given by @find_jobid.
732  * If any entries in the hash table match the value, they are removed
733  */
734 void lustre_jobid_clear(const char *find_jobid)
735 {
736         char jobid[LUSTRE_JOBID_SIZE];
737         char *end;
738
739         if (jobid_hash == NULL)
740                 return;
741
742         strlcpy(jobid, find_jobid, sizeof(jobid));
743         /* trim \n off the end of the incoming jobid */
744         end = strchr(jobid, '\n');
745         if (end && *end == '\n')
746                 *end = '\0';
747
748         CDEBUG(D_INFO, "Clearing Jobid: %s\n", jobid);
749         cfs_hash_cond_del(jobid_hash, jobid_should_free_item, jobid);
750
751         CDEBUG(D_INFO, "%d items remain in jobID table\n",
752                atomic_read(&jobid_hash->hs_count));
753 }