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 * Copyright (c) 2017, Intel Corporation.
28 * Author: Henri Doreau <henri.doreau@cea.fr>
31 #define DEBUG_SUBSYSTEM S_MDC
33 #include <linux/init.h>
34 #include <linux/kthread.h>
35 #include <linux/poll.h>
36 #include <linux/miscdevice.h>
38 #include <lustre_log.h>
39 #include <uapi/linux/lustre/lustre_ioctl.h>
41 #include "mdc_internal.h"
45 * -- Changelog delivery through character device --
49 * Mutex to protect chlg_registered_devices below
51 static DEFINE_MUTEX(chlg_registered_dev_lock);
54 * Global linked list of all registered devices (one per MDT).
56 static LIST_HEAD(chlg_registered_devices);
59 struct chlg_registered_dev {
60 /* Device name of the form "changelog-{MDTNAME}" */
62 /* Misc device descriptor */
63 struct miscdevice ced_misc;
64 /* OBDs referencing this device (multiple mount point) */
65 struct list_head ced_obds;
66 /* Reference counter for proper deregistration */
68 /* Link within the global chlg_registered_devices */
69 struct list_head ced_link;
72 struct chlg_reader_state {
73 /* Shortcut to the corresponding OBD device */
74 struct obd_device *crs_obd;
75 /* Producer thread (if any) */
76 struct task_struct *crs_prod_task;
77 /* An error occurred that prevents from reading further */
79 /* EOF, no more records available */
81 /* Desired start position */
82 __u64 crs_start_offset;
83 /* Wait queue for the catalog processing thread */
84 wait_queue_head_t crs_waitq_prod;
85 /* Wait queue for the record copy threads */
86 wait_queue_head_t crs_waitq_cons;
87 /* Mutex protecting crs_rec_count and crs_rec_queue */
88 struct mutex crs_lock;
89 /* Number of item in the list */
91 /* List of prefetched enqueued_record::enq_linkage_items */
92 struct list_head crs_rec_queue;
93 unsigned int crs_last_catidx;
94 unsigned int crs_last_idx;
98 struct chlg_rec_entry {
99 /* Link within the chlg_reader_state::crs_rec_queue list */
100 struct list_head enq_linkage;
101 /* Data (enq_record) field length */
103 /* Copy of a changelog record (see struct llog_changelog_rec) */
104 struct changelog_rec enq_record[];
108 /* Number of records to prefetch locally. */
109 CDEV_CHLG_MAX_PREFETCH = 1024,
113 * ChangeLog catalog processing callback invoked on each record.
114 * If the current record is eligible to userland delivery, push
115 * it into the crs_rec_queue where the consumer code will fetch it.
117 * @param[in] env (unused)
118 * @param[in] llh Client-side handle used to identify the llog
119 * @param[in] hdr Header of the current llog record
120 * @param[in,out] data chlg_reader_state passed from caller
122 * @return 0 or LLOG_PROC_* control code on success, negated error on failure.
124 static int chlg_read_cat_process_cb(const struct lu_env *env,
125 struct llog_handle *llh,
126 struct llog_rec_hdr *hdr, void *data)
128 struct llog_changelog_rec *rec;
129 struct chlg_reader_state *crs = data;
130 struct chlg_rec_entry *enq;
135 LASSERT(crs != NULL);
136 LASSERT(hdr != NULL);
138 rec = container_of(hdr, struct llog_changelog_rec, cr_hdr);
140 crs->crs_last_catidx = llh->lgh_hdr->llh_cat_idx;
141 crs->crs_last_idx = hdr->lrh_index;
143 if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
145 CERROR("%s: not a changelog rec %x/%d in llog "DFID" rc = %d\n",
146 crs->crs_obd->obd_name, rec->cr_hdr.lrh_type,
148 PFID(lu_object_fid(&llh->lgh_obj->do_lu)), rc);
152 /* Skip undesired records */
153 if (rec->cr.cr_index < crs->crs_start_offset)
156 CDEBUG(D_HSM, "%llu %02d%-5s %llu 0x%x t="DFID" p="DFID" %.*s\n",
157 rec->cr.cr_index, rec->cr.cr_type,
158 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
159 rec->cr.cr_flags & CLF_FLAGMASK,
160 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
161 rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
163 wait_event_interruptible(crs->crs_waitq_prod,
164 crs->crs_rec_count < CDEV_CHLG_MAX_PREFETCH ||
165 kthread_should_stop());
167 if (kthread_should_stop())
168 RETURN(LLOG_PROC_BREAK);
170 len = changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
171 OBD_ALLOC(enq, sizeof(*enq) + len);
175 INIT_LIST_HEAD(&enq->enq_linkage);
176 enq->enq_length = len;
177 memcpy(enq->enq_record, &rec->cr, len);
179 mutex_lock(&crs->crs_lock);
180 list_add_tail(&enq->enq_linkage, &crs->crs_rec_queue);
181 crs->crs_rec_count++;
182 mutex_unlock(&crs->crs_lock);
184 wake_up_all(&crs->crs_waitq_cons);
190 * Remove record from the list it is attached to and free it.
192 static void enq_record_delete(struct chlg_rec_entry *rec)
194 list_del(&rec->enq_linkage);
195 OBD_FREE(rec, sizeof(*rec) + rec->enq_length);
199 * Record prefetch thread entry point. Opens the changelog catalog and starts
202 * @param[in,out] args chlg_reader_state passed from caller.
203 * @return 0 on success, negated error code on failure.
205 static int chlg_load(void *args)
207 struct chlg_reader_state *crs = args;
208 struct obd_device *obd = crs->crs_obd;
209 struct llog_ctxt *ctx = NULL;
210 struct llog_handle *llh = NULL;
214 ctx = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
216 GOTO(err_out, rc = -ENOENT);
218 crs->crs_last_catidx = -1;
219 crs->crs_last_idx = 0;
222 rc = llog_open(NULL, ctx, &llh, NULL, CHANGELOG_CATALOG,
225 CERROR("%s: fail to open changelog catalog: rc = %d\n",
231 rc = llog_init_handle(NULL, llh,
234 LLOG_F_EXT_EXTRA_FLAGS |
235 LLOG_F_EXT_X_UIDGID |
241 CERROR("%s: fail to init llog handle: rc = %d\n",
246 rc = llog_cat_process(NULL, llh, chlg_read_cat_process_cb, crs,
247 crs->crs_last_catidx, crs->crs_last_idx);
249 CERROR("%s: fail to process llog: rc = %d\n", obd->obd_name, rc);
252 if (!kthread_should_stop() && crs->crs_poll) {
253 llog_cat_close(NULL, llh);
254 schedule_timeout_interruptible(HZ);
264 wake_up_all(&crs->crs_waitq_cons);
267 llog_cat_close(NULL, llh);
272 wait_event_interruptible(crs->crs_waitq_prod, kthread_should_stop());
278 * Read handler, dequeues records from the chlg_reader_state if any.
279 * No partial records are copied to userland so this function can return less
280 * data than required (short read).
282 * @param[in] file File pointer to the character device.
283 * @param[out] buff Userland buffer where to copy the records.
284 * @param[in] count Userland buffer size.
285 * @param[out] ppos File position, updated with the index number of the next
287 * @return number of copied bytes on success, negated error code on failure.
289 static ssize_t chlg_read(struct file *file, char __user *buff, size_t count,
292 struct chlg_reader_state *crs = file->private_data;
293 struct chlg_rec_entry *rec;
294 struct chlg_rec_entry *tmp;
295 size_t written_total = 0;
300 if (file->f_flags & O_NONBLOCK && crs->crs_rec_count == 0) {
301 if (crs->crs_err < 0)
302 RETURN(crs->crs_err);
303 else if (crs->crs_eof)
309 rc = wait_event_interruptible(crs->crs_waitq_cons,
310 crs->crs_rec_count > 0 || crs->crs_eof || crs->crs_err);
312 mutex_lock(&crs->crs_lock);
313 list_for_each_entry_safe(rec, tmp, &crs->crs_rec_queue, enq_linkage) {
314 if (written_total + rec->enq_length > count)
317 if (copy_to_user(buff, rec->enq_record, rec->enq_length)) {
322 buff += rec->enq_length;
323 written_total += rec->enq_length;
325 crs->crs_rec_count--;
326 list_move_tail(&rec->enq_linkage, &consumed);
328 crs->crs_start_offset = rec->enq_record->cr_index + 1;
330 mutex_unlock(&crs->crs_lock);
332 if (written_total > 0) {
334 wake_up_all(&crs->crs_waitq_prod);
335 } else if (rc == 0) {
339 list_for_each_entry_safe(rec, tmp, &consumed, enq_linkage)
340 enq_record_delete(rec);
342 *ppos = crs->crs_start_offset;
348 * Jump to a given record index. Helper for chlg_llseek().
350 * @param[in,out] crs Internal reader state.
351 * @param[in] offset Desired offset (index record).
352 * @return 0 on success, negated error code on failure.
354 static int chlg_set_start_offset(struct chlg_reader_state *crs, __u64 offset)
356 struct chlg_rec_entry *rec;
357 struct chlg_rec_entry *tmp;
359 mutex_lock(&crs->crs_lock);
360 if (offset < crs->crs_start_offset) {
361 mutex_unlock(&crs->crs_lock);
365 crs->crs_start_offset = offset;
366 list_for_each_entry_safe(rec, tmp, &crs->crs_rec_queue, enq_linkage) {
367 struct changelog_rec *cr = rec->enq_record;
369 if (cr->cr_index >= crs->crs_start_offset)
372 crs->crs_rec_count--;
373 enq_record_delete(rec);
376 mutex_unlock(&crs->crs_lock);
377 wake_up_all(&crs->crs_waitq_prod);
382 * Move read pointer to a certain record index, encoded as an offset.
384 * @param[in,out] file File pointer to the changelog character device
385 * @param[in] off Offset to skip, actually a record index, not byte count
386 * @param[in] whence Relative/Absolute interpretation of the offset
387 * @return the resulting position on success or negated error code on failure.
389 static loff_t chlg_llseek(struct file *file, loff_t off, int whence)
391 struct chlg_reader_state *crs = file->private_data;
400 pos = file->f_pos + off;
407 /* We cannot go backward */
408 if (pos < file->f_pos)
411 rc = chlg_set_start_offset(crs, pos);
420 * Clear record range for a given changelog reader.
422 * @param[in] crs Current internal state.
423 * @param[in] reader Changelog reader ID (cl1, cl2...)
424 * @param[in] record Record index up which to clear
425 * @return 0 on success, negated error code on failure.
427 static int chlg_clear(struct chlg_reader_state *crs, __u32 reader, __u64 record)
429 struct obd_device *obd = crs->crs_obd;
430 struct changelog_setinfo cs = {
435 return obd_set_info_async(NULL, obd->obd_self_export,
436 strlen(KEY_CHANGELOG_CLEAR),
437 KEY_CHANGELOG_CLEAR, sizeof(cs), &cs, NULL);
440 /** Maximum changelog control command size */
441 #define CHLG_CONTROL_CMD_MAX 64
444 * Handle writes() into the changelog character device. Write() can be used
445 * to request special control operations.
447 * @param[in] file File pointer to the changelog character device
448 * @param[in] buff User supplied data (written data)
449 * @param[in] count Number of written bytes
450 * @param[in] off (unused)
451 * @return number of written bytes on success, negated error code on failure.
453 static ssize_t chlg_write(struct file *file, const char __user *buff,
454 size_t count, loff_t *off)
456 struct chlg_reader_state *crs = file->private_data;
463 if (count > CHLG_CONTROL_CMD_MAX)
466 OBD_ALLOC(kbuf, CHLG_CONTROL_CMD_MAX);
470 if (copy_from_user(kbuf, buff, count))
471 GOTO(out_kbuf, rc = -EFAULT);
473 kbuf[CHLG_CONTROL_CMD_MAX - 1] = '\0';
475 if (sscanf(kbuf, "clear:cl%u:%llu", &reader, &record) == 2)
476 rc = chlg_clear(crs, reader, record);
482 OBD_FREE(kbuf, CHLG_CONTROL_CMD_MAX);
483 return rc < 0 ? rc : count;
487 * Open handler, initialize internal CRS state and spawn prefetch thread if
489 * @param[in] inode Inode struct for the open character device.
490 * @param[in] file Corresponding file pointer.
491 * @return 0 on success, negated error code on failure.
493 static int chlg_open(struct inode *inode, struct file *file)
495 struct chlg_reader_state *crs;
496 struct miscdevice *misc = file->private_data;
497 struct chlg_registered_dev *dev;
498 struct obd_device *obd;
499 struct task_struct *task;
503 dev = container_of(misc, struct chlg_registered_dev, ced_misc);
504 obd = list_first_entry(&dev->ced_obds,
506 u.cli.cl_chg_dev_linkage);
513 crs->crs_err = false;
514 crs->crs_eof = false;
516 mutex_init(&crs->crs_lock);
517 INIT_LIST_HEAD(&crs->crs_rec_queue);
518 init_waitqueue_head(&crs->crs_waitq_prod);
519 init_waitqueue_head(&crs->crs_waitq_cons);
521 if (file->f_mode & FMODE_READ) {
522 task = kthread_run(chlg_load, crs, "chlg_load_thread");
525 CERROR("%s: cannot start changelog thread: rc = %d\n",
529 crs->crs_prod_task = task;
532 file->private_data = crs;
541 * Close handler, release resources.
543 * @param[in] inode Inode struct for the open character device.
544 * @param[in] file Corresponding file pointer.
545 * @return 0 on success, negated error code on failure.
547 static int chlg_release(struct inode *inode, struct file *file)
549 struct chlg_reader_state *crs = file->private_data;
550 struct chlg_rec_entry *rec;
551 struct chlg_rec_entry *tmp;
554 if (crs->crs_prod_task)
555 rc = kthread_stop(crs->crs_prod_task);
557 list_for_each_entry_safe(rec, tmp, &crs->crs_rec_queue, enq_linkage)
558 enq_record_delete(rec);
566 * Poll handler, indicates whether the device is readable (new records) and
569 * @param[in] file Device file pointer.
570 * @param[in] wait (opaque)
571 * @return combination of the poll status flags.
573 static unsigned int chlg_poll(struct file *file, poll_table *wait)
575 struct chlg_reader_state *crs = file->private_data;
576 unsigned int mask = 0;
578 mutex_lock(&crs->crs_lock);
579 poll_wait(file, &crs->crs_waitq_cons, wait);
580 if (crs->crs_rec_count > 0)
581 mask |= POLLIN | POLLRDNORM;
586 mutex_unlock(&crs->crs_lock);
590 static long chlg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
594 struct chlg_reader_state *crs = file->private_data;
596 case OBD_IOC_CHLG_POLL:
597 crs->crs_poll = !!arg;
607 static const struct file_operations chlg_fops = {
608 .owner = THIS_MODULE,
609 .llseek = chlg_llseek,
613 .release = chlg_release,
615 .unlocked_ioctl = chlg_ioctl,
619 * This uses obd_name of the form: "testfs-MDT0000-mdc-ffff88006501600"
620 * and returns a name of the form: "changelog-testfs-MDT0000".
622 static void get_chlg_name(char *name, size_t name_len, struct obd_device *obd)
626 snprintf(name, name_len, "changelog-%s", obd->obd_name);
628 /* Find the 2nd '-' from the end and truncate on it */
629 for (i = 0; i < 2; i++) {
630 char *p = strrchr(name, '-');
639 * Find a changelog character device by name.
640 * All devices registered during MDC setup are listed in a global list with
641 * their names attached.
643 static struct chlg_registered_dev *
644 chlg_registered_dev_find_by_name(const char *name)
646 struct chlg_registered_dev *dit;
648 list_for_each_entry(dit, &chlg_registered_devices, ced_link)
649 if (strcmp(name, dit->ced_name) == 0)
655 * Find chlg_registered_dev structure for a given OBD device.
656 * This is bad O(n^2) but for each filesystem:
657 * - N is # of MDTs times # of mount points
658 * - this only runs at shutdown
660 static struct chlg_registered_dev *
661 chlg_registered_dev_find_by_obd(const struct obd_device *obd)
663 struct chlg_registered_dev *dit;
664 struct obd_device *oit;
666 list_for_each_entry(dit, &chlg_registered_devices, ced_link)
667 list_for_each_entry(oit, &dit->ced_obds,
668 u.cli.cl_chg_dev_linkage)
675 * Changelog character device initialization.
676 * Register a misc character device with a dynamic minor number, under a name
677 * of the form: 'changelog-fsname-MDTxxxx'. Reference this OBD device with it.
679 * @param[in] obd This MDC obd_device.
680 * @return 0 on success, negated error code on failure.
682 int mdc_changelog_cdev_init(struct obd_device *obd)
684 struct chlg_registered_dev *exist;
685 struct chlg_registered_dev *entry;
689 OBD_ALLOC_PTR(entry);
693 get_chlg_name(entry->ced_name, sizeof(entry->ced_name), obd);
695 entry->ced_misc.minor = MISC_DYNAMIC_MINOR;
696 entry->ced_misc.name = entry->ced_name;
697 entry->ced_misc.fops = &chlg_fops;
699 kref_init(&entry->ced_refs);
700 INIT_LIST_HEAD(&entry->ced_obds);
701 INIT_LIST_HEAD(&entry->ced_link);
703 mutex_lock(&chlg_registered_dev_lock);
704 exist = chlg_registered_dev_find_by_name(entry->ced_name);
706 kref_get(&exist->ced_refs);
707 list_add_tail(&obd->u.cli.cl_chg_dev_linkage, &exist->ced_obds);
708 GOTO(out_unlock, rc = 0);
711 list_add_tail(&obd->u.cli.cl_chg_dev_linkage, &entry->ced_obds);
712 list_add_tail(&entry->ced_link, &chlg_registered_devices);
714 /* Register new character device */
715 rc = misc_register(&entry->ced_misc);
717 list_del_init(&obd->u.cli.cl_chg_dev_linkage);
718 list_del(&entry->ced_link);
719 GOTO(out_unlock, rc);
722 entry = NULL; /* prevent it from being freed below */
725 mutex_unlock(&chlg_registered_dev_lock);
732 * Deregister a changelog character device whose refcount has reached zero.
734 static void chlg_dev_clear(struct kref *kref)
736 struct chlg_registered_dev *entry = container_of(kref,
737 struct chlg_registered_dev,
741 list_del(&entry->ced_link);
742 misc_deregister(&entry->ced_misc);
748 * Release OBD, decrease reference count of the corresponding changelog device.
750 void mdc_changelog_cdev_finish(struct obd_device *obd)
752 struct chlg_registered_dev *dev = chlg_registered_dev_find_by_obd(obd);
755 mutex_lock(&chlg_registered_dev_lock);
756 list_del_init(&obd->u.cli.cl_chg_dev_linkage);
757 kref_put(&dev->ced_refs, chlg_dev_clear);
758 mutex_unlock(&chlg_registered_dev_lock);