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