4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2017, Commissariat a l'Energie Atomique et aux Energies
26 * Author: Henri Doreau <henri.doreau@cea.fr>
29 #define DEBUG_SUBSYSTEM S_MDC
31 #include <linux/init.h>
32 #include <linux/kthread.h>
33 #include <linux/poll.h>
34 #include <linux/miscdevice.h>
36 #include <lustre_log.h>
38 #include "mdc_internal.h"
42 * -- Changelog delivery through character device --
46 * Mutex to protect chlg_registered_devices below
48 static DEFINE_MUTEX(chlg_registered_dev_lock);
51 * Global linked list of all registered devices (one per MDT).
53 static LIST_HEAD(chlg_registered_devices);
56 struct chlg_registered_dev {
57 /* Device name of the form "changelog-{MDTNAME}" */
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 */
65 /* Link within the global chlg_registered_devices */
66 struct list_head ced_link;
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 */
76 /* EOF, no more records available */
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 */
88 /* List of prefetched enqueued_record::enq_linkage_items */
89 struct list_head crs_rec_queue;
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 */
97 /* Copy of a changelog record (see struct llog_changelog_rec) */
98 struct changelog_rec enq_record[];
102 /* Number of records to prefetch locally. */
103 CDEV_CHLG_MAX_PREFETCH = 1024,
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.
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
116 * @return 0 or LLOG_PROC_* control code on success, negated error on failure.
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)
122 struct llog_changelog_rec *rec;
123 struct chlg_reader_state *crs = data;
124 struct chlg_rec_entry *enq;
129 LASSERT(crs != NULL);
130 LASSERT(hdr != NULL);
132 rec = container_of(hdr, struct llog_changelog_rec, cr_hdr);
134 if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
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,
139 PFID(lu_object_fid(&llh->lgh_obj->do_lu)), rc);
143 /* Skip undesired records */
144 if (rec->cr.cr_index < crs->crs_start_offset)
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));
154 wait_event_interruptible(crs->crs_waitq_prod,
155 crs->crs_rec_count < CDEV_CHLG_MAX_PREFETCH ||
156 kthread_should_stop());
158 if (kthread_should_stop())
159 RETURN(LLOG_PROC_BREAK);
161 len = changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
162 OBD_ALLOC(enq, sizeof(*enq) + len);
166 INIT_LIST_HEAD(&enq->enq_linkage);
167 enq->enq_length = len;
168 memcpy(enq->enq_record, &rec->cr, len);
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);
175 wake_up_all(&crs->crs_waitq_cons);
181 * Remove record from the list it is attached to and free it.
183 static void enq_record_delete(struct chlg_rec_entry *rec)
185 list_del(&rec->enq_linkage);
186 OBD_FREE(rec, sizeof(*rec) + rec->enq_length);
190 * Record prefetch thread entry point. Opens the changelog catalog and starts
193 * @param[in,out] args chlg_reader_state passed from caller.
194 * @return 0 on success, negated error code on failure.
196 static int chlg_load(void *args)
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;
205 ctx = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
207 GOTO(err_out, rc = -ENOENT);
209 rc = llog_open(NULL, ctx, &llh, NULL, CHANGELOG_CATALOG,
212 CERROR("%s: fail to open changelog catalog: rc = %d\n",
217 rc = llog_init_handle(NULL, llh, LLOG_F_IS_CAT|LLOG_F_EXT_JOBID, NULL);
219 CERROR("%s: fail to init llog handle: rc = %d\n",
224 rc = llog_cat_process(NULL, llh, chlg_read_cat_process_cb, crs, 0, 0);
226 CERROR("%s: fail to process llog: rc = %d\n", obd->obd_name, rc);
236 wake_up_all(&crs->crs_waitq_cons);
239 llog_cat_close(NULL, llh);
244 wait_event_interruptible(crs->crs_waitq_prod, kthread_should_stop());
250 * Read handler, dequeues records from the chlg_reader_state if any.
251 * No partial records are copied to userland so this function can return less
252 * data than required (short read).
254 * @param[in] file File pointer to the character device.
255 * @param[out] buff Userland buffer where to copy the records.
256 * @param[in] count Userland buffer size.
257 * @param[out] ppos File position, updated with the index number of the next
259 * @return number of copied bytes on success, negated error code on failure.
261 static ssize_t chlg_read(struct file *file, char __user *buff, size_t count,
264 struct chlg_reader_state *crs = file->private_data;
265 struct chlg_rec_entry *rec;
266 struct chlg_rec_entry *tmp;
267 size_t written_total = 0;
272 if (file->f_flags & O_NONBLOCK && crs->crs_rec_count == 0) {
273 if (crs->crs_err < 0)
274 RETURN(crs->crs_err);
275 else if (crs->crs_eof)
281 rc = wait_event_interruptible(crs->crs_waitq_cons,
282 crs->crs_rec_count > 0 || crs->crs_eof || crs->crs_err);
284 mutex_lock(&crs->crs_lock);
285 list_for_each_entry_safe(rec, tmp, &crs->crs_rec_queue, enq_linkage) {
286 if (written_total + rec->enq_length > count)
289 if (copy_to_user(buff, rec->enq_record, rec->enq_length)) {
294 buff += rec->enq_length;
295 written_total += rec->enq_length;
297 crs->crs_rec_count--;
298 list_move_tail(&rec->enq_linkage, &consumed);
300 crs->crs_start_offset = rec->enq_record->cr_index + 1;
302 mutex_unlock(&crs->crs_lock);
304 if (written_total > 0) {
306 wake_up_all(&crs->crs_waitq_prod);
307 } else if (rc == 0) {
311 list_for_each_entry_safe(rec, tmp, &consumed, enq_linkage)
312 enq_record_delete(rec);
314 *ppos = crs->crs_start_offset;
320 * Jump to a given record index. Helper for chlg_llseek().
322 * @param[in,out] crs Internal reader state.
323 * @param[in] offset Desired offset (index record).
324 * @return 0 on success, negated error code on failure.
326 static int chlg_set_start_offset(struct chlg_reader_state *crs, __u64 offset)
328 struct chlg_rec_entry *rec;
329 struct chlg_rec_entry *tmp;
331 mutex_lock(&crs->crs_lock);
332 if (offset < crs->crs_start_offset) {
333 mutex_unlock(&crs->crs_lock);
337 crs->crs_start_offset = offset;
338 list_for_each_entry_safe(rec, tmp, &crs->crs_rec_queue, enq_linkage) {
339 struct changelog_rec *cr = rec->enq_record;
341 if (cr->cr_index >= crs->crs_start_offset)
344 crs->crs_rec_count--;
345 enq_record_delete(rec);
348 mutex_unlock(&crs->crs_lock);
349 wake_up_all(&crs->crs_waitq_prod);
354 * Move read pointer to a certain record index, encoded as an offset.
356 * @param[in,out] file File pointer to the changelog character device
357 * @param[in] off Offset to skip, actually a record index, not byte count
358 * @param[in] whence Relative/Absolute interpretation of the offset
359 * @return the resulting position on success or negated error code on failure.
361 static loff_t chlg_llseek(struct file *file, loff_t off, int whence)
363 struct chlg_reader_state *crs = file->private_data;
372 pos = file->f_pos + off;
379 /* We cannot go backward */
380 if (pos < file->f_pos)
383 rc = chlg_set_start_offset(crs, pos);
392 * Clear record range for a given changelog reader.
394 * @param[in] crs Current internal state.
395 * @param[in] reader Changelog reader ID (cl1, cl2...)
396 * @param[in] record Record index up which to clear
397 * @return 0 on success, negated error code on failure.
399 static int chlg_clear(struct chlg_reader_state *crs, __u32 reader, __u64 record)
401 struct obd_device *obd = crs->crs_obd;
402 struct changelog_setinfo cs = {
407 return obd_set_info_async(NULL, obd->obd_self_export,
408 strlen(KEY_CHANGELOG_CLEAR),
409 KEY_CHANGELOG_CLEAR, sizeof(cs), &cs, NULL);
412 /** Maximum changelog control command size */
413 #define CHLG_CONTROL_CMD_MAX 64
416 * Handle writes() into the changelog character device. Write() can be used
417 * to request special control operations.
419 * @param[in] file File pointer to the changelog character device
420 * @param[in] buff User supplied data (written data)
421 * @param[in] count Number of written bytes
422 * @param[in] off (unused)
423 * @return number of written bytes on success, negated error code on failure.
425 static ssize_t chlg_write(struct file *file, const char __user *buff,
426 size_t count, loff_t *off)
428 struct chlg_reader_state *crs = file->private_data;
435 if (count > CHLG_CONTROL_CMD_MAX)
438 OBD_ALLOC(kbuf, CHLG_CONTROL_CMD_MAX);
442 if (copy_from_user(kbuf, buff, count))
443 GOTO(out_kbuf, rc = -EFAULT);
445 kbuf[CHLG_CONTROL_CMD_MAX - 1] = '\0';
447 if (sscanf(kbuf, "clear:cl%u:%llu", &reader, &record) == 2)
448 rc = chlg_clear(crs, reader, record);
454 OBD_FREE(kbuf, CHLG_CONTROL_CMD_MAX);
455 return rc < 0 ? rc : count;
459 * Find the OBD device associated to a changelog character device.
460 * @param[in] cdev character device instance descriptor
461 * @return corresponding OBD device or NULL if none was found.
463 static struct obd_device *chlg_obd_get(dev_t cdev)
465 int minor = MINOR(cdev);
466 struct obd_device *obd = NULL;
467 struct chlg_registered_dev *curr;
469 mutex_lock(&chlg_registered_dev_lock);
470 list_for_each_entry(curr, &chlg_registered_devices, ced_link) {
471 if (curr->ced_misc.minor == minor) {
472 /* take the first available OBD device attached */
473 obd = list_first_entry(&curr->ced_obds,
475 u.cli.cl_chg_dev_linkage);
479 mutex_unlock(&chlg_registered_dev_lock);
484 * Open handler, initialize internal CRS state and spawn prefetch thread if
486 * @param[in] inode Inode struct for the open character device.
487 * @param[in] file Corresponding file pointer.
488 * @return 0 on success, negated error code on failure.
490 static int chlg_open(struct inode *inode, struct file *file)
492 struct chlg_reader_state *crs;
493 struct obd_device *obd = chlg_obd_get(inode->i_rdev);
494 struct task_struct *task;
506 crs->crs_err = false;
507 crs->crs_eof = false;
509 mutex_init(&crs->crs_lock);
510 INIT_LIST_HEAD(&crs->crs_rec_queue);
511 init_waitqueue_head(&crs->crs_waitq_prod);
512 init_waitqueue_head(&crs->crs_waitq_cons);
514 if (file->f_mode & FMODE_READ) {
515 task = kthread_run(chlg_load, crs, "chlg_load_thread");
518 CERROR("%s: cannot start changelog thread: rc = %d\n",
522 crs->crs_prod_task = task;
525 file->private_data = crs;
534 * Close handler, release resources.
536 * @param[in] inode Inode struct for the open character device.
537 * @param[in] file Corresponding file pointer.
538 * @return 0 on success, negated error code on failure.
540 static int chlg_release(struct inode *inode, struct file *file)
542 struct chlg_reader_state *crs = file->private_data;
543 struct chlg_rec_entry *rec;
544 struct chlg_rec_entry *tmp;
547 if (crs->crs_prod_task)
548 rc = kthread_stop(crs->crs_prod_task);
550 list_for_each_entry_safe(rec, tmp, &crs->crs_rec_queue, enq_linkage)
551 enq_record_delete(rec);
559 * Poll handler, indicates whether the device is readable (new records) and
562 * @param[in] file Device file pointer.
563 * @param[in] wait (opaque)
564 * @return combination of the poll status flags.
566 static unsigned int chlg_poll(struct file *file, poll_table *wait)
568 struct chlg_reader_state *crs = file->private_data;
569 unsigned int mask = 0;
571 mutex_lock(&crs->crs_lock);
572 poll_wait(file, &crs->crs_waitq_cons, wait);
573 if (crs->crs_rec_count > 0)
574 mask |= POLLIN | POLLRDNORM;
579 mutex_unlock(&crs->crs_lock);
583 static const struct file_operations chlg_fops = {
584 .owner = THIS_MODULE,
585 .llseek = chlg_llseek,
589 .release = chlg_release,
594 * This uses obd_name of the form: "testfs-MDT0000-mdc-ffff88006501600"
595 * and returns a name of the form: "changelog-testfs-MDT0000".
597 static void get_chlg_name(char *name, size_t name_len, struct obd_device *obd)
601 snprintf(name, name_len, "changelog-%s", obd->obd_name);
603 /* Find the 2nd '-' from the end and truncate on it */
604 for (i = 0; i < 2; i++) {
605 char *p = strrchr(name, '-');
614 * Find a changelog character device by name.
615 * All devices registered during MDC setup are listed in a global list with
616 * their names attached.
618 static struct chlg_registered_dev *
619 chlg_registered_dev_find_by_name(const char *name)
621 struct chlg_registered_dev *dit;
623 list_for_each_entry(dit, &chlg_registered_devices, ced_link)
624 if (strcmp(name, dit->ced_name) == 0)
630 * Find chlg_registered_dev structure for a given OBD device.
631 * This is bad O(n^2) but for each filesystem:
632 * - N is # of MDTs times # of mount points
633 * - this only runs at shutdown
635 static struct chlg_registered_dev *
636 chlg_registered_dev_find_by_obd(const struct obd_device *obd)
638 struct chlg_registered_dev *dit;
639 struct obd_device *oit;
641 list_for_each_entry(dit, &chlg_registered_devices, ced_link)
642 list_for_each_entry(oit, &dit->ced_obds,
643 u.cli.cl_chg_dev_linkage)
650 * Changelog character device initialization.
651 * Register a misc character device with a dynamic minor number, under a name
652 * of the form: 'changelog-fsname-MDTxxxx'. Reference this OBD device with it.
654 * @param[in] obd This MDC obd_device.
655 * @return 0 on success, negated error code on failure.
657 int mdc_changelog_cdev_init(struct obd_device *obd)
659 struct chlg_registered_dev *exist;
660 struct chlg_registered_dev *entry;
664 OBD_ALLOC_PTR(entry);
668 get_chlg_name(entry->ced_name, sizeof(entry->ced_name), obd);
670 entry->ced_misc.minor = MISC_DYNAMIC_MINOR;
671 entry->ced_misc.name = entry->ced_name;
672 entry->ced_misc.fops = &chlg_fops;
674 kref_init(&entry->ced_refs);
675 INIT_LIST_HEAD(&entry->ced_obds);
676 INIT_LIST_HEAD(&entry->ced_link);
678 mutex_lock(&chlg_registered_dev_lock);
679 exist = chlg_registered_dev_find_by_name(entry->ced_name);
681 kref_get(&exist->ced_refs);
682 list_add_tail(&obd->u.cli.cl_chg_dev_linkage, &exist->ced_obds);
683 GOTO(out_unlock, rc = 0);
686 /* Register new character device */
687 rc = misc_register(&entry->ced_misc);
689 GOTO(out_unlock, rc);
691 list_add_tail(&obd->u.cli.cl_chg_dev_linkage, &entry->ced_obds);
692 list_add_tail(&entry->ced_link, &chlg_registered_devices);
694 entry = NULL; /* prevent it from being freed below */
697 mutex_unlock(&chlg_registered_dev_lock);
704 * Deregister a changelog character device whose refcount has reached zero.
706 static void chlg_dev_clear(struct kref *kref)
708 struct chlg_registered_dev *entry = container_of(kref,
709 struct chlg_registered_dev,
713 list_del(&entry->ced_link);
714 misc_deregister(&entry->ced_misc);
720 * Release OBD, decrease reference count of the corresponding changelog device.
722 void mdc_changelog_cdev_finish(struct obd_device *obd)
724 struct chlg_registered_dev *dev = chlg_registered_dev_find_by_obd(obd);
727 mutex_lock(&chlg_registered_dev_lock);
728 list_del_init(&obd->u.cli.cl_chg_dev_linkage);
729 kref_put(&dev->ced_refs, chlg_dev_clear);
730 mutex_unlock(&chlg_registered_dev_lock);