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