Whamcloud - gitweb
LU-9727 lustre: add client NID 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  * 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                               LLOG_F_EXT_X_NID,
225                               NULL);
226         if (rc) {
227                 CERROR("%s: fail to init llog handle: rc = %d\n",
228                        obd->obd_name, rc);
229                 GOTO(err_out, rc);
230         }
231
232         rc = llog_cat_process(NULL, llh, chlg_read_cat_process_cb, crs, 0, 0);
233         if (rc < 0) {
234                 CERROR("%s: fail to process llog: rc = %d\n", obd->obd_name, rc);
235                 GOTO(err_out, rc);
236         }
237
238         crs->crs_eof = true;
239
240 err_out:
241         if (rc < 0)
242                 crs->crs_err = rc;
243
244         wake_up_all(&crs->crs_waitq_cons);
245
246         if (llh != NULL)
247                 llog_cat_close(NULL, llh);
248
249         if (ctx != NULL)
250                 llog_ctxt_put(ctx);
251
252         wait_event_interruptible(crs->crs_waitq_prod, kthread_should_stop());
253
254         RETURN(rc);
255 }
256
257 /**
258  * Read handler, dequeues records from the chlg_reader_state if any.
259  * No partial records are copied to userland so this function can return less
260  * data than required (short read).
261  *
262  * @param[in]   file   File pointer to the character device.
263  * @param[out]  buff   Userland buffer where to copy the records.
264  * @param[in]   count  Userland buffer size.
265  * @param[out]  ppos   File position, updated with the index number of the next
266  *                     record to read.
267  * @return number of copied bytes on success, negated error code on failure.
268  */
269 static ssize_t chlg_read(struct file *file, char __user *buff, size_t count,
270                          loff_t *ppos)
271 {
272         struct chlg_reader_state *crs = file->private_data;
273         struct chlg_rec_entry *rec;
274         struct chlg_rec_entry *tmp;
275         size_t written_total = 0;
276         ssize_t rc;
277         LIST_HEAD(consumed);
278         ENTRY;
279
280         if (file->f_flags & O_NONBLOCK && crs->crs_rec_count == 0) {
281                 if (crs->crs_err < 0)
282                         RETURN(crs->crs_err);
283                 else if (crs->crs_eof)
284                         RETURN(0);
285                 else
286                         RETURN(-EAGAIN);
287         }
288
289         rc = wait_event_interruptible(crs->crs_waitq_cons,
290                         crs->crs_rec_count > 0 || crs->crs_eof || crs->crs_err);
291
292         mutex_lock(&crs->crs_lock);
293         list_for_each_entry_safe(rec, tmp, &crs->crs_rec_queue, enq_linkage) {
294                 if (written_total + rec->enq_length > count)
295                         break;
296
297                 if (copy_to_user(buff, rec->enq_record, rec->enq_length)) {
298                         rc = -EFAULT;
299                         break;
300                 }
301
302                 buff += rec->enq_length;
303                 written_total += rec->enq_length;
304
305                 crs->crs_rec_count--;
306                 list_move_tail(&rec->enq_linkage, &consumed);
307
308                 crs->crs_start_offset = rec->enq_record->cr_index + 1;
309         }
310         mutex_unlock(&crs->crs_lock);
311
312         if (written_total > 0) {
313                 rc = written_total;
314                 wake_up_all(&crs->crs_waitq_prod);
315         } else if (rc == 0) {
316                 rc = crs->crs_err;
317         }
318
319         list_for_each_entry_safe(rec, tmp, &consumed, enq_linkage)
320                 enq_record_delete(rec);
321
322         *ppos = crs->crs_start_offset;
323
324         RETURN(rc);
325 }
326
327 /**
328  * Jump to a given record index. Helper for chlg_llseek().
329  *
330  * @param[in,out]  crs     Internal reader state.
331  * @param[in]      offset  Desired offset (index record).
332  * @return 0 on success, negated error code on failure.
333  */
334 static int chlg_set_start_offset(struct chlg_reader_state *crs, __u64 offset)
335 {
336         struct chlg_rec_entry *rec;
337         struct chlg_rec_entry *tmp;
338
339         mutex_lock(&crs->crs_lock);
340         if (offset < crs->crs_start_offset) {
341                 mutex_unlock(&crs->crs_lock);
342                 return -ERANGE;
343         }
344
345         crs->crs_start_offset = offset;
346         list_for_each_entry_safe(rec, tmp, &crs->crs_rec_queue, enq_linkage) {
347                 struct changelog_rec *cr = rec->enq_record;
348
349                 if (cr->cr_index >= crs->crs_start_offset)
350                         break;
351
352                 crs->crs_rec_count--;
353                 enq_record_delete(rec);
354         }
355
356         mutex_unlock(&crs->crs_lock);
357         wake_up_all(&crs->crs_waitq_prod);
358         return 0;
359 }
360
361 /**
362  * Move read pointer to a certain record index, encoded as an offset.
363  *
364  * @param[in,out] file   File pointer to the changelog character device
365  * @param[in]     off    Offset to skip, actually a record index, not byte count
366  * @param[in]     whence Relative/Absolute interpretation of the offset
367  * @return the resulting position on success or negated error code on failure.
368  */
369 static loff_t chlg_llseek(struct file *file, loff_t off, int whence)
370 {
371         struct chlg_reader_state *crs = file->private_data;
372         loff_t pos;
373         int rc;
374
375         switch (whence) {
376         case SEEK_SET:
377                 pos = off;
378                 break;
379         case SEEK_CUR:
380                 pos = file->f_pos + off;
381                 break;
382         case SEEK_END:
383         default:
384                 return -EINVAL;
385         }
386
387         /* We cannot go backward */
388         if (pos < file->f_pos)
389                 return -EINVAL;
390
391         rc = chlg_set_start_offset(crs, pos);
392         if (rc != 0)
393                 return rc;
394
395         file->f_pos = pos;
396         return pos;
397 }
398
399 /**
400  * Clear record range for a given changelog reader.
401  *
402  * @param[in]  crs     Current internal state.
403  * @param[in]  reader  Changelog reader ID (cl1, cl2...)
404  * @param[in]  record  Record index up which to clear
405  * @return 0 on success, negated error code on failure.
406  */
407 static int chlg_clear(struct chlg_reader_state *crs, __u32 reader, __u64 record)
408 {
409         struct obd_device *obd = crs->crs_obd;
410         struct changelog_setinfo cs  = {
411                 .cs_recno = record,
412                 .cs_id    = reader
413         };
414
415         return obd_set_info_async(NULL, obd->obd_self_export,
416                                   strlen(KEY_CHANGELOG_CLEAR),
417                                   KEY_CHANGELOG_CLEAR, sizeof(cs), &cs, NULL);
418 }
419
420 /** Maximum changelog control command size */
421 #define CHLG_CONTROL_CMD_MAX    64
422
423 /**
424  * Handle writes() into the changelog character device. Write() can be used
425  * to request special control operations.
426  *
427  * @param[in]  file  File pointer to the changelog character device
428  * @param[in]  buff  User supplied data (written data)
429  * @param[in]  count Number of written bytes
430  * @param[in]  off   (unused)
431  * @return number of written bytes on success, negated error code on failure.
432  */
433 static ssize_t chlg_write(struct file *file, const char __user *buff,
434                           size_t count, loff_t *off)
435 {
436         struct chlg_reader_state *crs = file->private_data;
437         char *kbuf;
438         __u64 record;
439         __u32 reader;
440         int rc = 0;
441         ENTRY;
442
443         if (count > CHLG_CONTROL_CMD_MAX)
444                 RETURN(-EINVAL);
445
446         OBD_ALLOC(kbuf, CHLG_CONTROL_CMD_MAX);
447         if (kbuf == NULL)
448                 RETURN(-ENOMEM);
449
450         if (copy_from_user(kbuf, buff, count))
451                 GOTO(out_kbuf, rc = -EFAULT);
452
453         kbuf[CHLG_CONTROL_CMD_MAX - 1] = '\0';
454
455         if (sscanf(kbuf, "clear:cl%u:%llu", &reader, &record) == 2)
456                 rc = chlg_clear(crs, reader, record);
457         else
458                 rc = -EINVAL;
459
460         EXIT;
461 out_kbuf:
462         OBD_FREE(kbuf, CHLG_CONTROL_CMD_MAX);
463         return rc < 0 ? rc : count;
464 }
465
466 /**
467  * Find the OBD device associated to a changelog character device.
468  * @param[in]  cdev  character device instance descriptor
469  * @return corresponding OBD device or NULL if none was found.
470  */
471 static struct obd_device *chlg_obd_get(dev_t cdev)
472 {
473         int minor = MINOR(cdev);
474         struct obd_device *obd = NULL;
475         struct chlg_registered_dev *curr;
476
477         mutex_lock(&chlg_registered_dev_lock);
478         list_for_each_entry(curr, &chlg_registered_devices, ced_link) {
479                 if (curr->ced_misc.minor == minor) {
480                         /* take the first available OBD device attached */
481                         obd = list_first_entry(&curr->ced_obds,
482                                                struct obd_device,
483                                                u.cli.cl_chg_dev_linkage);
484                         break;
485                 }
486         }
487         mutex_unlock(&chlg_registered_dev_lock);
488         return obd;
489 }
490
491 /**
492  * Open handler, initialize internal CRS state and spawn prefetch thread if
493  * needed.
494  * @param[in]  inode  Inode struct for the open character device.
495  * @param[in]  file   Corresponding file pointer.
496  * @return 0 on success, negated error code on failure.
497  */
498 static int chlg_open(struct inode *inode, struct file *file)
499 {
500         struct chlg_reader_state *crs;
501         struct obd_device *obd = chlg_obd_get(inode->i_rdev);
502         struct task_struct *task;
503         int rc;
504         ENTRY;
505
506         if (!obd)
507                 RETURN(-ENODEV);
508
509         OBD_ALLOC_PTR(crs);
510         if (!crs)
511                 RETURN(-ENOMEM);
512
513         crs->crs_obd = obd;
514         crs->crs_err = false;
515         crs->crs_eof = false;
516
517         mutex_init(&crs->crs_lock);
518         INIT_LIST_HEAD(&crs->crs_rec_queue);
519         init_waitqueue_head(&crs->crs_waitq_prod);
520         init_waitqueue_head(&crs->crs_waitq_cons);
521
522         if (file->f_mode & FMODE_READ) {
523                 task = kthread_run(chlg_load, crs, "chlg_load_thread");
524                 if (IS_ERR(task)) {
525                         rc = PTR_ERR(task);
526                         CERROR("%s: cannot start changelog thread: rc = %d\n",
527                                obd->obd_name, rc);
528                         GOTO(err_crs, rc);
529                 }
530                 crs->crs_prod_task = task;
531         }
532
533         file->private_data = crs;
534         RETURN(0);
535
536 err_crs:
537         OBD_FREE_PTR(crs);
538         return rc;
539 }
540
541 /**
542  * Close handler, release resources.
543  *
544  * @param[in]  inode  Inode struct for the open character device.
545  * @param[in]  file   Corresponding file pointer.
546  * @return 0 on success, negated error code on failure.
547  */
548 static int chlg_release(struct inode *inode, struct file *file)
549 {
550         struct chlg_reader_state *crs = file->private_data;
551         struct chlg_rec_entry *rec;
552         struct chlg_rec_entry *tmp;
553         int rc = 0;
554
555         if (crs->crs_prod_task)
556                 rc = kthread_stop(crs->crs_prod_task);
557
558         list_for_each_entry_safe(rec, tmp, &crs->crs_rec_queue, enq_linkage)
559                 enq_record_delete(rec);
560
561         OBD_FREE_PTR(crs);
562
563         return rc;
564 }
565
566 /**
567  * Poll handler, indicates whether the device is readable (new records) and
568  * writable (always).
569  *
570  * @param[in]  file   Device file pointer.
571  * @param[in]  wait   (opaque)
572  * @return combination of the poll status flags.
573  */
574 static unsigned int chlg_poll(struct file *file, poll_table *wait)
575 {
576         struct chlg_reader_state *crs = file->private_data;
577         unsigned int mask = 0;
578
579         mutex_lock(&crs->crs_lock);
580         poll_wait(file, &crs->crs_waitq_cons, wait);
581         if (crs->crs_rec_count > 0)
582                 mask |= POLLIN | POLLRDNORM;
583         if (crs->crs_err)
584                 mask |= POLLERR;
585         if (crs->crs_eof)
586                 mask |= POLLHUP;
587         mutex_unlock(&crs->crs_lock);
588         return mask;
589 }
590
591 static const struct file_operations chlg_fops = {
592         .owner          = THIS_MODULE,
593         .llseek         = chlg_llseek,
594         .read           = chlg_read,
595         .write          = chlg_write,
596         .open           = chlg_open,
597         .release        = chlg_release,
598         .poll           = chlg_poll,
599 };
600
601 /**
602  * This uses obd_name of the form: "testfs-MDT0000-mdc-ffff88006501600"
603  * and returns a name of the form: "changelog-testfs-MDT0000".
604  */
605 static void get_chlg_name(char *name, size_t name_len, struct obd_device *obd)
606 {
607         int i;
608
609         snprintf(name, name_len, "changelog-%s", obd->obd_name);
610
611         /* Find the 2nd '-' from the end and truncate on it */
612         for (i = 0; i < 2; i++) {
613                 char *p = strrchr(name, '-');
614
615                 if (p == NULL)
616                         return;
617                 *p = '\0';
618         }
619 }
620
621 /**
622  * Find a changelog character device by name.
623  * All devices registered during MDC setup are listed in a global list with
624  * their names attached.
625  */
626 static struct chlg_registered_dev *
627 chlg_registered_dev_find_by_name(const char *name)
628 {
629         struct chlg_registered_dev *dit;
630
631         list_for_each_entry(dit, &chlg_registered_devices, ced_link)
632                 if (strcmp(name, dit->ced_name) == 0)
633                         return dit;
634         return NULL;
635 }
636
637 /**
638  * Find chlg_registered_dev structure for a given OBD device.
639  * This is bad O(n^2) but for each filesystem:
640  *   - N is # of MDTs times # of mount points
641  *   - this only runs at shutdown
642  */
643 static struct chlg_registered_dev *
644 chlg_registered_dev_find_by_obd(const struct obd_device *obd)
645 {
646         struct chlg_registered_dev *dit;
647         struct obd_device *oit;
648
649         list_for_each_entry(dit, &chlg_registered_devices, ced_link)
650                 list_for_each_entry(oit, &dit->ced_obds,
651                                     u.cli.cl_chg_dev_linkage)
652                         if (oit == obd)
653                                 return dit;
654         return NULL;
655 }
656
657 /**
658  * Changelog character device initialization.
659  * Register a misc character device with a dynamic minor number, under a name
660  * of the form: 'changelog-fsname-MDTxxxx'. Reference this OBD device with it.
661  *
662  * @param[in] obd  This MDC obd_device.
663  * @return 0 on success, negated error code on failure.
664  */
665 int mdc_changelog_cdev_init(struct obd_device *obd)
666 {
667         struct chlg_registered_dev *exist;
668         struct chlg_registered_dev *entry;
669         int rc;
670         ENTRY;
671
672         OBD_ALLOC_PTR(entry);
673         if (entry == NULL)
674                 RETURN(-ENOMEM);
675
676         get_chlg_name(entry->ced_name, sizeof(entry->ced_name), obd);
677
678         entry->ced_misc.minor = MISC_DYNAMIC_MINOR;
679         entry->ced_misc.name  = entry->ced_name;
680         entry->ced_misc.fops  = &chlg_fops;
681
682         kref_init(&entry->ced_refs);
683         INIT_LIST_HEAD(&entry->ced_obds);
684         INIT_LIST_HEAD(&entry->ced_link);
685
686         mutex_lock(&chlg_registered_dev_lock);
687         exist = chlg_registered_dev_find_by_name(entry->ced_name);
688         if (exist != NULL) {
689                 kref_get(&exist->ced_refs);
690                 list_add_tail(&obd->u.cli.cl_chg_dev_linkage, &exist->ced_obds);
691                 GOTO(out_unlock, rc = 0);
692         }
693
694         /* Register new character device */
695         rc = misc_register(&entry->ced_misc);
696         if (rc != 0)
697                 GOTO(out_unlock, rc);
698
699         list_add_tail(&obd->u.cli.cl_chg_dev_linkage, &entry->ced_obds);
700         list_add_tail(&entry->ced_link, &chlg_registered_devices);
701
702         entry = NULL;   /* prevent it from being freed below */
703
704 out_unlock:
705         mutex_unlock(&chlg_registered_dev_lock);
706         if (entry)
707                 OBD_FREE_PTR(entry);
708         RETURN(rc);
709 }
710
711 /**
712  * Deregister a changelog character device whose refcount has reached zero.
713  */
714 static void chlg_dev_clear(struct kref *kref)
715 {
716         struct chlg_registered_dev *entry = container_of(kref,
717                                                       struct chlg_registered_dev,
718                                                       ced_refs);
719         ENTRY;
720
721         list_del(&entry->ced_link);
722         misc_deregister(&entry->ced_misc);
723         OBD_FREE_PTR(entry);
724         EXIT;
725 }
726
727 /**
728  * Release OBD, decrease reference count of the corresponding changelog device.
729  */
730 void mdc_changelog_cdev_finish(struct obd_device *obd)
731 {
732         struct chlg_registered_dev *dev = chlg_registered_dev_find_by_obd(obd);
733         ENTRY;
734
735         mutex_lock(&chlg_registered_dev_lock);
736         list_del_init(&obd->u.cli.cl_chg_dev_linkage);
737         kref_put(&dev->ced_refs, chlg_dev_clear);
738         mutex_unlock(&chlg_registered_dev_lock);
739         EXIT;
740 }