Whamcloud - gitweb
LU-10308 misc: update Intel copyright messages for 2017
[fs/lustre-release.git] / lustre / mdc / mdc_changelog.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) 2017, Commissariat a l'Energie Atomique et aux Energies
24  *                     Alternatives.
25  *
26  * Copyright (c) 2017, Intel Corporation.
27  *
28  * Author: Henri Doreau <henri.doreau@cea.fr>
29  */
30
31 #define DEBUG_SUBSYSTEM S_MDC
32
33 #include <linux/init.h>
34 #include <linux/kthread.h>
35 #include <linux/poll.h>
36 #include <linux/miscdevice.h>
37
38 #include <lustre_log.h>
39
40 #include "mdc_internal.h"
41
42
43 /*
44  * -- Changelog delivery through character device --
45  */
46
47 /**
48  * Mutex to protect chlg_registered_devices below
49  */
50 static DEFINE_MUTEX(chlg_registered_dev_lock);
51
52 /**
53  * Global linked list of all registered devices (one per MDT).
54  */
55 static LIST_HEAD(chlg_registered_devices);
56
57
58 struct chlg_registered_dev {
59         /* Device name of the form "changelog-{MDTNAME}" */
60         char                    ced_name[32];
61         /* Misc device descriptor */
62         struct miscdevice       ced_misc;
63         /* OBDs referencing this device (multiple mount point) */
64         struct list_head        ced_obds;
65         /* Reference counter for proper deregistration */
66         struct kref             ced_refs;
67         /* Link within the global chlg_registered_devices */
68         struct list_head        ced_link;
69 };
70
71 struct chlg_reader_state {
72         /* Shortcut to the corresponding OBD device */
73         struct obd_device       *crs_obd;
74         /* Producer thread (if any) */
75         struct task_struct      *crs_prod_task;
76         /* An error occurred that prevents from reading further */
77         int                      crs_err;
78         /* EOF, no more records available */
79         bool                     crs_eof;
80         /* Desired start position */
81         __u64                    crs_start_offset;
82         /* Wait queue for the catalog processing thread */
83         wait_queue_head_t        crs_waitq_prod;
84         /* Wait queue for the record copy threads */
85         wait_queue_head_t        crs_waitq_cons;
86         /* Mutex protecting crs_rec_count and crs_rec_queue */
87         struct mutex             crs_lock;
88         /* Number of item in the list */
89         __u64                    crs_rec_count;
90         /* List of prefetched enqueued_record::enq_linkage_items */
91         struct list_head         crs_rec_queue;
92 };
93
94 struct chlg_rec_entry {
95         /* Link within the chlg_reader_state::crs_rec_queue list */
96         struct list_head        enq_linkage;
97         /* Data (enq_record) field length */
98         __u64                   enq_length;
99         /* Copy of a changelog record (see struct llog_changelog_rec) */
100         struct changelog_rec    enq_record[];
101 };
102
103 enum {
104         /* Number of records to prefetch locally. */
105         CDEV_CHLG_MAX_PREFETCH = 1024,
106 };
107
108 /**
109  * ChangeLog catalog processing callback invoked on each record.
110  * If the current record is eligible to userland delivery, push
111  * it into the crs_rec_queue where the consumer code will fetch it.
112  *
113  * @param[in]     env  (unused)
114  * @param[in]     llh  Client-side handle used to identify the llog
115  * @param[in]     hdr  Header of the current llog record
116  * @param[in,out] data chlg_reader_state passed from caller
117  *
118  * @return 0 or LLOG_PROC_* control code on success, negated error on failure.
119  */
120 static int chlg_read_cat_process_cb(const struct lu_env *env,
121                                     struct llog_handle *llh,
122                                     struct llog_rec_hdr *hdr, void *data)
123 {
124         struct llog_changelog_rec *rec;
125         struct chlg_reader_state *crs = data;
126         struct chlg_rec_entry *enq;
127         size_t len;
128         int rc;
129         ENTRY;
130
131         LASSERT(crs != NULL);
132         LASSERT(hdr != NULL);
133
134         rec = container_of(hdr, struct llog_changelog_rec, cr_hdr);
135
136         if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
137                 rc = -EINVAL;
138                 CERROR("%s: not a changelog rec %x/%d in llog "DFID" rc = %d\n",
139                        crs->crs_obd->obd_name, rec->cr_hdr.lrh_type,
140                        rec->cr.cr_type,
141                        PFID(lu_object_fid(&llh->lgh_obj->do_lu)), rc);
142                 RETURN(rc);
143         }
144
145         /* Skip undesired records */
146         if (rec->cr.cr_index < crs->crs_start_offset)
147                 RETURN(0);
148
149         CDEBUG(D_HSM, "%llu %02d%-5s %llu 0x%x t="DFID" p="DFID" %.*s\n",
150                rec->cr.cr_index, rec->cr.cr_type,
151                changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
152                rec->cr.cr_flags & CLF_FLAGMASK,
153                PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
154                rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
155
156         wait_event_interruptible(crs->crs_waitq_prod,
157                                  crs->crs_rec_count < CDEV_CHLG_MAX_PREFETCH ||
158                                  kthread_should_stop());
159
160         if (kthread_should_stop())
161                 RETURN(LLOG_PROC_BREAK);
162
163         len = changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
164         OBD_ALLOC(enq, sizeof(*enq) + len);
165         if (enq == NULL)
166                 RETURN(-ENOMEM);
167
168         INIT_LIST_HEAD(&enq->enq_linkage);
169         enq->enq_length = len;
170         memcpy(enq->enq_record, &rec->cr, len);
171
172         mutex_lock(&crs->crs_lock);
173         list_add_tail(&enq->enq_linkage, &crs->crs_rec_queue);
174         crs->crs_rec_count++;
175         mutex_unlock(&crs->crs_lock);
176
177         wake_up_all(&crs->crs_waitq_cons);
178
179         RETURN(0);
180 }
181
182 /**
183  * Remove record from the list it is attached to and free it.
184  */
185 static void enq_record_delete(struct chlg_rec_entry *rec)
186 {
187         list_del(&rec->enq_linkage);
188         OBD_FREE(rec, sizeof(*rec) + rec->enq_length);
189 }
190
191 /**
192  * Record prefetch thread entry point. Opens the changelog catalog and starts
193  * reading records.
194  *
195  * @param[in,out]  args  chlg_reader_state passed from caller.
196  * @return 0 on success, negated error code on failure.
197  */
198 static int chlg_load(void *args)
199 {
200         struct chlg_reader_state *crs = args;
201         struct obd_device *obd = crs->crs_obd;
202         struct llog_ctxt *ctx = NULL;
203         struct llog_handle *llh = NULL;
204         int rc;
205         ENTRY;
206
207         ctx = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
208         if (ctx == NULL)
209                 GOTO(err_out, rc = -ENOENT);
210
211         rc = llog_open(NULL, ctx, &llh, NULL, CHANGELOG_CATALOG,
212                        LLOG_OPEN_EXISTS);
213         if (rc) {
214                 CERROR("%s: fail to open changelog catalog: rc = %d\n",
215                        obd->obd_name, rc);
216                 GOTO(err_out, rc);
217         }
218
219         rc = llog_init_handle(NULL, llh,
220                               LLOG_F_IS_CAT |
221                               LLOG_F_EXT_JOBID |
222                               LLOG_F_EXT_EXTRA_FLAGS |
223                               LLOG_F_EXT_X_UIDGID,
224                               NULL);
225         if (rc) {
226                 CERROR("%s: fail to init llog handle: rc = %d\n",
227                        obd->obd_name, rc);
228                 GOTO(err_out, rc);
229         }
230
231         rc = llog_cat_process(NULL, llh, chlg_read_cat_process_cb, crs, 0, 0);
232         if (rc < 0) {
233                 CERROR("%s: fail to process llog: rc = %d\n", obd->obd_name, rc);
234                 GOTO(err_out, rc);
235         }
236
237         crs->crs_eof = true;
238
239 err_out:
240         if (rc < 0)
241                 crs->crs_err = rc;
242
243         wake_up_all(&crs->crs_waitq_cons);
244
245         if (llh != NULL)
246                 llog_cat_close(NULL, llh);
247
248         if (ctx != NULL)
249                 llog_ctxt_put(ctx);
250
251         wait_event_interruptible(crs->crs_waitq_prod, kthread_should_stop());
252
253         RETURN(rc);
254 }
255
256 /**
257  * Read handler, dequeues records from the chlg_reader_state if any.
258  * No partial records are copied to userland so this function can return less
259  * data than required (short read).
260  *
261  * @param[in]   file   File pointer to the character device.
262  * @param[out]  buff   Userland buffer where to copy the records.
263  * @param[in]   count  Userland buffer size.
264  * @param[out]  ppos   File position, updated with the index number of the next
265  *                     record to read.
266  * @return number of copied bytes on success, negated error code on failure.
267  */
268 static ssize_t chlg_read(struct file *file, char __user *buff, size_t count,
269                          loff_t *ppos)
270 {
271         struct chlg_reader_state *crs = file->private_data;
272         struct chlg_rec_entry *rec;
273         struct chlg_rec_entry *tmp;
274         size_t written_total = 0;
275         ssize_t rc;
276         LIST_HEAD(consumed);
277         ENTRY;
278
279         if (file->f_flags & O_NONBLOCK && crs->crs_rec_count == 0) {
280                 if (crs->crs_err < 0)
281                         RETURN(crs->crs_err);
282                 else if (crs->crs_eof)
283                         RETURN(0);
284                 else
285                         RETURN(-EAGAIN);
286         }
287
288         rc = wait_event_interruptible(crs->crs_waitq_cons,
289                         crs->crs_rec_count > 0 || crs->crs_eof || crs->crs_err);
290
291         mutex_lock(&crs->crs_lock);
292         list_for_each_entry_safe(rec, tmp, &crs->crs_rec_queue, enq_linkage) {
293                 if (written_total + rec->enq_length > count)
294                         break;
295
296                 if (copy_to_user(buff, rec->enq_record, rec->enq_length)) {
297                         rc = -EFAULT;
298                         break;
299                 }
300
301                 buff += rec->enq_length;
302                 written_total += rec->enq_length;
303
304                 crs->crs_rec_count--;
305                 list_move_tail(&rec->enq_linkage, &consumed);
306
307                 crs->crs_start_offset = rec->enq_record->cr_index + 1;
308         }
309         mutex_unlock(&crs->crs_lock);
310
311         if (written_total > 0) {
312                 rc = written_total;
313                 wake_up_all(&crs->crs_waitq_prod);
314         } else if (rc == 0) {
315                 rc = crs->crs_err;
316         }
317
318         list_for_each_entry_safe(rec, tmp, &consumed, enq_linkage)
319                 enq_record_delete(rec);
320
321         *ppos = crs->crs_start_offset;
322
323         RETURN(rc);
324 }
325
326 /**
327  * Jump to a given record index. Helper for chlg_llseek().
328  *
329  * @param[in,out]  crs     Internal reader state.
330  * @param[in]      offset  Desired offset (index record).
331  * @return 0 on success, negated error code on failure.
332  */
333 static int chlg_set_start_offset(struct chlg_reader_state *crs, __u64 offset)
334 {
335         struct chlg_rec_entry *rec;
336         struct chlg_rec_entry *tmp;
337
338         mutex_lock(&crs->crs_lock);
339         if (offset < crs->crs_start_offset) {
340                 mutex_unlock(&crs->crs_lock);
341                 return -ERANGE;
342         }
343
344         crs->crs_start_offset = offset;
345         list_for_each_entry_safe(rec, tmp, &crs->crs_rec_queue, enq_linkage) {
346                 struct changelog_rec *cr = rec->enq_record;
347
348                 if (cr->cr_index >= crs->crs_start_offset)
349                         break;
350
351                 crs->crs_rec_count--;
352                 enq_record_delete(rec);
353         }
354
355         mutex_unlock(&crs->crs_lock);
356         wake_up_all(&crs->crs_waitq_prod);
357         return 0;
358 }
359
360 /**
361  * Move read pointer to a certain record index, encoded as an offset.
362  *
363  * @param[in,out] file   File pointer to the changelog character device
364  * @param[in]     off    Offset to skip, actually a record index, not byte count
365  * @param[in]     whence Relative/Absolute interpretation of the offset
366  * @return the resulting position on success or negated error code on failure.
367  */
368 static loff_t chlg_llseek(struct file *file, loff_t off, int whence)
369 {
370         struct chlg_reader_state *crs = file->private_data;
371         loff_t pos;
372         int rc;
373
374         switch (whence) {
375         case SEEK_SET:
376                 pos = off;
377                 break;
378         case SEEK_CUR:
379                 pos = file->f_pos + off;
380                 break;
381         case SEEK_END:
382         default:
383                 return -EINVAL;
384         }
385
386         /* We cannot go backward */
387         if (pos < file->f_pos)
388                 return -EINVAL;
389
390         rc = chlg_set_start_offset(crs, pos);
391         if (rc != 0)
392                 return rc;
393
394         file->f_pos = pos;
395         return pos;
396 }
397
398 /**
399  * Clear record range for a given changelog reader.
400  *
401  * @param[in]  crs     Current internal state.
402  * @param[in]  reader  Changelog reader ID (cl1, cl2...)
403  * @param[in]  record  Record index up which to clear
404  * @return 0 on success, negated error code on failure.
405  */
406 static int chlg_clear(struct chlg_reader_state *crs, __u32 reader, __u64 record)
407 {
408         struct obd_device *obd = crs->crs_obd;
409         struct changelog_setinfo cs  = {
410                 .cs_recno = record,
411                 .cs_id    = reader
412         };
413
414         return obd_set_info_async(NULL, obd->obd_self_export,
415                                   strlen(KEY_CHANGELOG_CLEAR),
416                                   KEY_CHANGELOG_CLEAR, sizeof(cs), &cs, NULL);
417 }
418
419 /** Maximum changelog control command size */
420 #define CHLG_CONTROL_CMD_MAX    64
421
422 /**
423  * Handle writes() into the changelog character device. Write() can be used
424  * to request special control operations.
425  *
426  * @param[in]  file  File pointer to the changelog character device
427  * @param[in]  buff  User supplied data (written data)
428  * @param[in]  count Number of written bytes
429  * @param[in]  off   (unused)
430  * @return number of written bytes on success, negated error code on failure.
431  */
432 static ssize_t chlg_write(struct file *file, const char __user *buff,
433                           size_t count, loff_t *off)
434 {
435         struct chlg_reader_state *crs = file->private_data;
436         char *kbuf;
437         __u64 record;
438         __u32 reader;
439         int rc = 0;
440         ENTRY;
441
442         if (count > CHLG_CONTROL_CMD_MAX)
443                 RETURN(-EINVAL);
444
445         OBD_ALLOC(kbuf, CHLG_CONTROL_CMD_MAX);
446         if (kbuf == NULL)
447                 RETURN(-ENOMEM);
448
449         if (copy_from_user(kbuf, buff, count))
450                 GOTO(out_kbuf, rc = -EFAULT);
451
452         kbuf[CHLG_CONTROL_CMD_MAX - 1] = '\0';
453
454         if (sscanf(kbuf, "clear:cl%u:%llu", &reader, &record) == 2)
455                 rc = chlg_clear(crs, reader, record);
456         else
457                 rc = -EINVAL;
458
459         EXIT;
460 out_kbuf:
461         OBD_FREE(kbuf, CHLG_CONTROL_CMD_MAX);
462         return rc < 0 ? rc : count;
463 }
464
465 /**
466  * Find the OBD device associated to a changelog character device.
467  * @param[in]  cdev  character device instance descriptor
468  * @return corresponding OBD device or NULL if none was found.
469  */
470 static struct obd_device *chlg_obd_get(dev_t cdev)
471 {
472         int minor = MINOR(cdev);
473         struct obd_device *obd = NULL;
474         struct chlg_registered_dev *curr;
475
476         mutex_lock(&chlg_registered_dev_lock);
477         list_for_each_entry(curr, &chlg_registered_devices, ced_link) {
478                 if (curr->ced_misc.minor == minor) {
479                         /* take the first available OBD device attached */
480                         obd = list_first_entry(&curr->ced_obds,
481                                                struct obd_device,
482                                                u.cli.cl_chg_dev_linkage);
483                         break;
484                 }
485         }
486         mutex_unlock(&chlg_registered_dev_lock);
487         return obd;
488 }
489
490 /**
491  * Open handler, initialize internal CRS state and spawn prefetch thread if
492  * needed.
493  * @param[in]  inode  Inode struct for the open character device.
494  * @param[in]  file   Corresponding file pointer.
495  * @return 0 on success, negated error code on failure.
496  */
497 static int chlg_open(struct inode *inode, struct file *file)
498 {
499         struct chlg_reader_state *crs;
500         struct obd_device *obd = chlg_obd_get(inode->i_rdev);
501         struct task_struct *task;
502         int rc;
503         ENTRY;
504
505         if (!obd)
506                 RETURN(-ENODEV);
507
508         OBD_ALLOC_PTR(crs);
509         if (!crs)
510                 RETURN(-ENOMEM);
511
512         crs->crs_obd = obd;
513         crs->crs_err = false;
514         crs->crs_eof = false;
515
516         mutex_init(&crs->crs_lock);
517         INIT_LIST_HEAD(&crs->crs_rec_queue);
518         init_waitqueue_head(&crs->crs_waitq_prod);
519         init_waitqueue_head(&crs->crs_waitq_cons);
520
521         if (file->f_mode & FMODE_READ) {
522                 task = kthread_run(chlg_load, crs, "chlg_load_thread");
523                 if (IS_ERR(task)) {
524                         rc = PTR_ERR(task);
525                         CERROR("%s: cannot start changelog thread: rc = %d\n",
526                                obd->obd_name, rc);
527                         GOTO(err_crs, rc);
528                 }
529                 crs->crs_prod_task = task;
530         }
531
532         file->private_data = crs;
533         RETURN(0);
534
535 err_crs:
536         OBD_FREE_PTR(crs);
537         return rc;
538 }
539
540 /**
541  * Close handler, release resources.
542  *
543  * @param[in]  inode  Inode struct for the open character device.
544  * @param[in]  file   Corresponding file pointer.
545  * @return 0 on success, negated error code on failure.
546  */
547 static int chlg_release(struct inode *inode, struct file *file)
548 {
549         struct chlg_reader_state *crs = file->private_data;
550         struct chlg_rec_entry *rec;
551         struct chlg_rec_entry *tmp;
552         int rc = 0;
553
554         if (crs->crs_prod_task)
555                 rc = kthread_stop(crs->crs_prod_task);
556
557         list_for_each_entry_safe(rec, tmp, &crs->crs_rec_queue, enq_linkage)
558                 enq_record_delete(rec);
559
560         OBD_FREE_PTR(crs);
561
562         return rc;
563 }
564
565 /**
566  * Poll handler, indicates whether the device is readable (new records) and
567  * writable (always).
568  *
569  * @param[in]  file   Device file pointer.
570  * @param[in]  wait   (opaque)
571  * @return combination of the poll status flags.
572  */
573 static unsigned int chlg_poll(struct file *file, poll_table *wait)
574 {
575         struct chlg_reader_state *crs = file->private_data;
576         unsigned int mask = 0;
577
578         mutex_lock(&crs->crs_lock);
579         poll_wait(file, &crs->crs_waitq_cons, wait);
580         if (crs->crs_rec_count > 0)
581                 mask |= POLLIN | POLLRDNORM;
582         if (crs->crs_err)
583                 mask |= POLLERR;
584         if (crs->crs_eof)
585                 mask |= POLLHUP;
586         mutex_unlock(&crs->crs_lock);
587         return mask;
588 }
589
590 static const struct file_operations chlg_fops = {
591         .owner          = THIS_MODULE,
592         .llseek         = chlg_llseek,
593         .read           = chlg_read,
594         .write          = chlg_write,
595         .open           = chlg_open,
596         .release        = chlg_release,
597         .poll           = chlg_poll,
598 };
599
600 /**
601  * This uses obd_name of the form: "testfs-MDT0000-mdc-ffff88006501600"
602  * and returns a name of the form: "changelog-testfs-MDT0000".
603  */
604 static void get_chlg_name(char *name, size_t name_len, struct obd_device *obd)
605 {
606         int i;
607
608         snprintf(name, name_len, "changelog-%s", obd->obd_name);
609
610         /* Find the 2nd '-' from the end and truncate on it */
611         for (i = 0; i < 2; i++) {
612                 char *p = strrchr(name, '-');
613
614                 if (p == NULL)
615                         return;
616                 *p = '\0';
617         }
618 }
619
620 /**
621  * Find a changelog character device by name.
622  * All devices registered during MDC setup are listed in a global list with
623  * their names attached.
624  */
625 static struct chlg_registered_dev *
626 chlg_registered_dev_find_by_name(const char *name)
627 {
628         struct chlg_registered_dev *dit;
629
630         list_for_each_entry(dit, &chlg_registered_devices, ced_link)
631                 if (strcmp(name, dit->ced_name) == 0)
632                         return dit;
633         return NULL;
634 }
635
636 /**
637  * Find chlg_registered_dev structure for a given OBD device.
638  * This is bad O(n^2) but for each filesystem:
639  *   - N is # of MDTs times # of mount points
640  *   - this only runs at shutdown
641  */
642 static struct chlg_registered_dev *
643 chlg_registered_dev_find_by_obd(const struct obd_device *obd)
644 {
645         struct chlg_registered_dev *dit;
646         struct obd_device *oit;
647
648         list_for_each_entry(dit, &chlg_registered_devices, ced_link)
649                 list_for_each_entry(oit, &dit->ced_obds,
650                                     u.cli.cl_chg_dev_linkage)
651                         if (oit == obd)
652                                 return dit;
653         return NULL;
654 }
655
656 /**
657  * Changelog character device initialization.
658  * Register a misc character device with a dynamic minor number, under a name
659  * of the form: 'changelog-fsname-MDTxxxx'. Reference this OBD device with it.
660  *
661  * @param[in] obd  This MDC obd_device.
662  * @return 0 on success, negated error code on failure.
663  */
664 int mdc_changelog_cdev_init(struct obd_device *obd)
665 {
666         struct chlg_registered_dev *exist;
667         struct chlg_registered_dev *entry;
668         int rc;
669         ENTRY;
670
671         OBD_ALLOC_PTR(entry);
672         if (entry == NULL)
673                 RETURN(-ENOMEM);
674
675         get_chlg_name(entry->ced_name, sizeof(entry->ced_name), obd);
676
677         entry->ced_misc.minor = MISC_DYNAMIC_MINOR;
678         entry->ced_misc.name  = entry->ced_name;
679         entry->ced_misc.fops  = &chlg_fops;
680
681         kref_init(&entry->ced_refs);
682         INIT_LIST_HEAD(&entry->ced_obds);
683         INIT_LIST_HEAD(&entry->ced_link);
684
685         mutex_lock(&chlg_registered_dev_lock);
686         exist = chlg_registered_dev_find_by_name(entry->ced_name);
687         if (exist != NULL) {
688                 kref_get(&exist->ced_refs);
689                 list_add_tail(&obd->u.cli.cl_chg_dev_linkage, &exist->ced_obds);
690                 GOTO(out_unlock, rc = 0);
691         }
692
693         /* Register new character device */
694         rc = misc_register(&entry->ced_misc);
695         if (rc != 0)
696                 GOTO(out_unlock, rc);
697
698         list_add_tail(&obd->u.cli.cl_chg_dev_linkage, &entry->ced_obds);
699         list_add_tail(&entry->ced_link, &chlg_registered_devices);
700
701         entry = NULL;   /* prevent it from being freed below */
702
703 out_unlock:
704         mutex_unlock(&chlg_registered_dev_lock);
705         if (entry)
706                 OBD_FREE_PTR(entry);
707         RETURN(rc);
708 }
709
710 /**
711  * Deregister a changelog character device whose refcount has reached zero.
712  */
713 static void chlg_dev_clear(struct kref *kref)
714 {
715         struct chlg_registered_dev *entry = container_of(kref,
716                                                       struct chlg_registered_dev,
717                                                       ced_refs);
718         ENTRY;
719
720         list_del(&entry->ced_link);
721         misc_deregister(&entry->ced_misc);
722         OBD_FREE_PTR(entry);
723         EXIT;
724 }
725
726 /**
727  * Release OBD, decrease reference count of the corresponding changelog device.
728  */
729 void mdc_changelog_cdev_finish(struct obd_device *obd)
730 {
731         struct chlg_registered_dev *dev = chlg_registered_dev_find_by_obd(obd);
732         ENTRY;
733
734         mutex_lock(&chlg_registered_dev_lock);
735         list_del_init(&obd->u.cli.cl_chg_dev_linkage);
736         kref_put(&dev->ced_refs, chlg_dev_clear);
737         mutex_unlock(&chlg_registered_dev_lock);
738         EXIT;
739 }