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/device.h>
37 #include <linux/cdev.h>
39 #include <lustre_log.h>
40 #include <uapi/linux/lustre/lustre_ioctl.h>
42 #include "mdc_internal.h"
46 * -- Changelog delivery through character device --
50 * Mutex to protect chlg_registered_devices below
52 static DEFINE_MUTEX(chlg_registered_dev_lock);
55 * Global linked list of all registered devices (one per MDT).
57 static LIST_HEAD(chlg_registered_devices);
60 struct chlg_registered_dev {
61 /* Device name of the form "changelog-{MDTNAME}" */
63 /* changelog char device */
65 struct device ced_device;
66 /* OBDs referencing this device (multiple mount point) */
67 struct list_head ced_obds;
68 /* Reference counter for proper deregistration */
70 /* Link within the global chlg_registered_devices */
71 struct list_head ced_link;
74 struct chlg_reader_state {
75 /* Shortcut to the corresponding OBD device */
76 struct obd_device *crs_obd;
77 /* the corresponding chlg_registered_dev */
78 struct chlg_registered_dev *crs_ced;
79 /* Producer thread (if any) */
80 struct task_struct *crs_prod_task;
81 /* An error occurred that prevents from reading further */
83 /* EOF, no more records available */
85 /* Desired start position */
86 __u64 crs_start_offset;
87 /* Wait queue for the catalog processing thread */
88 wait_queue_head_t crs_waitq_prod;
89 /* Wait queue for the record copy threads */
90 wait_queue_head_t crs_waitq_cons;
91 /* Mutex protecting crs_rec_count and crs_rec_queue */
92 struct mutex crs_lock;
93 /* Number of item in the list */
95 /* List of prefetched enqueued_record::enq_linkage_items */
96 struct list_head crs_rec_queue;
97 unsigned int crs_last_catidx;
98 unsigned int crs_last_idx;
102 struct chlg_rec_entry {
103 /* Link within the chlg_reader_state::crs_rec_queue list */
104 struct list_head enq_linkage;
105 /* Data (enq_record) field length */
107 /* Copy of a changelog record (see struct llog_changelog_rec) */
108 struct changelog_rec enq_record[];
112 /* Number of records to prefetch locally. */
113 CDEV_CHLG_MAX_PREFETCH = 1024,
116 DEFINE_IDR(mdc_changelog_minor_idr);
117 static DEFINE_SPINLOCK(chlg_minor_lock);
119 static int chlg_minor_alloc(int *pminor)
121 void *minor_allocated = (void *)-1;
124 idr_preload(GFP_KERNEL);
125 spin_lock(&chlg_minor_lock);
126 minor = idr_alloc(&mdc_changelog_minor_idr, minor_allocated, 0,
127 MDC_CHANGELOG_DEV_COUNT, GFP_NOWAIT);
128 spin_unlock(&chlg_minor_lock);
138 static void chlg_minor_free(int minor)
140 spin_lock(&chlg_minor_lock);
141 idr_remove(&mdc_changelog_minor_idr, minor);
142 spin_unlock(&chlg_minor_lock);
145 static void chlg_device_release(struct device *dev)
147 struct chlg_registered_dev *entry = dev_get_drvdata(dev);
149 chlg_minor_free(MINOR(entry->ced_cdev.dev));
154 * Deregister a changelog character device whose refcount has reached zero.
156 static void chlg_dev_clear(struct kref *kref)
158 struct chlg_registered_dev *entry;
161 entry = container_of(kref, struct chlg_registered_dev,
164 list_del(&entry->ced_link);
165 cdev_device_del(&entry->ced_cdev, &entry->ced_device);
166 put_device(&entry->ced_device);
170 static inline struct obd_device* chlg_obd_get(struct chlg_registered_dev *dev)
172 struct obd_device *obd;
174 mutex_lock(&chlg_registered_dev_lock);
175 if (list_empty(&dev->ced_obds))
178 obd = list_first_entry(&dev->ced_obds, struct obd_device,
179 u.cli.cl_chg_dev_linkage);
180 class_incref(obd, "changelog", dev);
181 mutex_unlock(&chlg_registered_dev_lock);
185 static inline void chlg_obd_put(struct chlg_registered_dev *dev,
186 struct obd_device *obd)
188 class_decref(obd, "changelog", dev);
192 * ChangeLog catalog processing callback invoked on each record.
193 * If the current record is eligible to userland delivery, push
194 * it into the crs_rec_queue where the consumer code will fetch it.
196 * @param[in] env (unused)
197 * @param[in] llh Client-side handle used to identify the llog
198 * @param[in] hdr Header of the current llog record
199 * @param[in,out] data chlg_reader_state passed from caller
201 * @return 0 or LLOG_PROC_* control code on success, negated error on failure.
203 static int chlg_read_cat_process_cb(const struct lu_env *env,
204 struct llog_handle *llh,
205 struct llog_rec_hdr *hdr, void *data)
207 struct llog_changelog_rec *rec;
208 struct chlg_reader_state *crs = data;
209 struct chlg_rec_entry *enq;
214 LASSERT(crs != NULL);
215 LASSERT(hdr != NULL);
217 rec = container_of(hdr, struct llog_changelog_rec, cr_hdr);
219 crs->crs_last_catidx = llh->lgh_hdr->llh_cat_idx;
220 crs->crs_last_idx = hdr->lrh_index;
222 if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
224 CERROR("%s: not a changelog rec %x/%d in llog "DFID" rc = %d\n",
225 crs->crs_obd->obd_name, rec->cr_hdr.lrh_type,
227 PFID(lu_object_fid(&llh->lgh_obj->do_lu)), rc);
231 /* Skip undesired records */
232 if (rec->cr.cr_index < crs->crs_start_offset)
235 CDEBUG(D_HSM, "%llu %02d%-5s %llu 0x%x t="DFID" p="DFID" %.*s\n",
236 rec->cr.cr_index, rec->cr.cr_type,
237 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
238 rec->cr.cr_flags & CLF_FLAGMASK,
239 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
240 rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
242 wait_event_interruptible(crs->crs_waitq_prod,
243 crs->crs_rec_count < CDEV_CHLG_MAX_PREFETCH ||
244 kthread_should_stop());
246 if (kthread_should_stop())
247 RETURN(LLOG_PROC_BREAK);
249 len = changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
250 OBD_ALLOC(enq, sizeof(*enq) + len);
254 INIT_LIST_HEAD(&enq->enq_linkage);
255 enq->enq_length = len;
256 memcpy(enq->enq_record, &rec->cr, len);
258 mutex_lock(&crs->crs_lock);
259 list_add_tail(&enq->enq_linkage, &crs->crs_rec_queue);
260 crs->crs_rec_count++;
261 mutex_unlock(&crs->crs_lock);
263 wake_up_all(&crs->crs_waitq_cons);
269 * Remove record from the list it is attached to and free it.
271 static void enq_record_delete(struct chlg_rec_entry *rec)
273 list_del(&rec->enq_linkage);
274 OBD_FREE(rec, sizeof(*rec) + rec->enq_length);
278 * Record prefetch thread entry point. Opens the changelog catalog and starts
281 * @param[in,out] args chlg_reader_state passed from caller.
282 * @return 0 on success, negated error code on failure.
284 static int chlg_load(void *args)
286 struct chlg_reader_state *crs = args;
287 struct chlg_registered_dev *ced = crs->crs_ced;
288 struct obd_device *obd = NULL;
289 struct llog_ctxt *ctx = NULL;
290 struct llog_handle *llh = NULL;
294 crs->crs_last_catidx = 0;
295 crs->crs_last_idx = 0;
298 obd = chlg_obd_get(ced);
304 ctx = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
306 GOTO(err_out, rc = -ENOENT);
308 rc = llog_open(NULL, ctx, &llh, NULL, CHANGELOG_CATALOG,
311 CERROR("%s: fail to open changelog catalog: rc = %d\n",
317 rc = llog_init_handle(NULL, llh,
320 LLOG_F_EXT_EXTRA_FLAGS |
321 LLOG_F_EXT_X_UIDGID |
327 CERROR("%s: fail to init llog handle: rc = %d\n",
332 rc = llog_cat_process(NULL, llh, chlg_read_cat_process_cb, crs,
333 crs->crs_last_catidx, crs->crs_last_idx);
335 CERROR("%s: fail to process llog: rc = %d\n", obd->obd_name, rc);
338 if (!kthread_should_stop() && crs->crs_poll) {
339 llog_cat_close(NULL, llh);
341 class_decref(obd, "changelog", crs);
342 schedule_timeout_interruptible(cfs_time_seconds(1));
352 wake_up_all(&crs->crs_waitq_cons);
355 llog_cat_close(NULL, llh);
361 chlg_obd_put(ced, obd);
362 wait_event_interruptible(crs->crs_waitq_prod, kthread_should_stop());
368 * Read handler, dequeues records from the chlg_reader_state if any.
369 * No partial records are copied to userland so this function can return less
370 * data than required (short read).
372 * @param[in] file File pointer to the character device.
373 * @param[out] buff Userland buffer where to copy the records.
374 * @param[in] count Userland buffer size.
375 * @param[out] ppos File position, updated with the index number of the next
377 * @return number of copied bytes on success, negated error code on failure.
379 static ssize_t chlg_read(struct file *file, char __user *buff, size_t count,
382 struct chlg_reader_state *crs = file->private_data;
383 struct chlg_rec_entry *rec;
384 struct chlg_rec_entry *tmp;
385 size_t written_total = 0;
390 if (file->f_flags & O_NONBLOCK && crs->crs_rec_count == 0) {
391 if (crs->crs_err < 0)
392 RETURN(crs->crs_err);
393 else if (crs->crs_eof)
399 rc = wait_event_interruptible(crs->crs_waitq_cons,
400 crs->crs_rec_count > 0 || crs->crs_eof || crs->crs_err);
402 mutex_lock(&crs->crs_lock);
403 list_for_each_entry_safe(rec, tmp, &crs->crs_rec_queue, enq_linkage) {
404 if (written_total + rec->enq_length > count)
407 if (copy_to_user(buff, rec->enq_record, rec->enq_length)) {
412 buff += rec->enq_length;
413 written_total += rec->enq_length;
415 crs->crs_rec_count--;
416 list_move_tail(&rec->enq_linkage, &consumed);
418 crs->crs_start_offset = rec->enq_record->cr_index + 1;
420 mutex_unlock(&crs->crs_lock);
422 if (written_total > 0) {
424 wake_up_all(&crs->crs_waitq_prod);
425 } else if (rc == 0) {
429 list_for_each_entry_safe(rec, tmp, &consumed, enq_linkage)
430 enq_record_delete(rec);
432 *ppos = crs->crs_start_offset;
438 * Jump to a given record index. Helper for chlg_llseek().
440 * @param[in,out] crs Internal reader state.
441 * @param[in] offset Desired offset (index record).
442 * @return 0 on success, negated error code on failure.
444 static int chlg_set_start_offset(struct chlg_reader_state *crs, __u64 offset)
446 struct chlg_rec_entry *rec;
447 struct chlg_rec_entry *tmp;
449 mutex_lock(&crs->crs_lock);
450 if (offset < crs->crs_start_offset) {
451 mutex_unlock(&crs->crs_lock);
455 crs->crs_start_offset = offset;
456 list_for_each_entry_safe(rec, tmp, &crs->crs_rec_queue, enq_linkage) {
457 struct changelog_rec *cr = rec->enq_record;
459 if (cr->cr_index >= crs->crs_start_offset)
462 crs->crs_rec_count--;
463 enq_record_delete(rec);
466 mutex_unlock(&crs->crs_lock);
467 wake_up_all(&crs->crs_waitq_prod);
472 * Move read pointer to a certain record index, encoded as an offset.
474 * @param[in,out] file File pointer to the changelog character device
475 * @param[in] off Offset to skip, actually a record index, not byte count
476 * @param[in] whence Relative/Absolute interpretation of the offset
477 * @return the resulting position on success or negated error code on failure.
479 static loff_t chlg_llseek(struct file *file, loff_t off, int whence)
481 struct chlg_reader_state *crs = file->private_data;
490 pos = file->f_pos + off;
497 /* We cannot go backward */
498 if (pos < file->f_pos)
501 rc = chlg_set_start_offset(crs, pos);
510 * Clear record range for a given changelog reader.
512 * @param[in] crs Current internal state.
513 * @param[in] reader Changelog reader ID (cl1, cl2...)
514 * @param[in] record Record index up which to clear
515 * @return 0 on success, negated error code on failure.
517 static int chlg_clear(struct chlg_reader_state *crs, __u32 reader, __u64 record)
519 struct obd_device *obd = NULL;
520 struct changelog_setinfo cs = {
526 obd = chlg_obd_get(crs->crs_ced);
530 rc = obd_set_info_async(NULL, obd->obd_self_export,
531 strlen(KEY_CHANGELOG_CLEAR),
532 KEY_CHANGELOG_CLEAR, sizeof(cs), &cs, NULL);
534 chlg_obd_put(crs->crs_ced, obd);
538 /** Maximum changelog control command size */
539 #define CHLG_CONTROL_CMD_MAX 64
542 * Handle writes() into the changelog character device. Write() can be used
543 * to request special control operations.
545 * @param[in] file File pointer to the changelog character device
546 * @param[in] buff User supplied data (written data)
547 * @param[in] count Number of written bytes
548 * @param[in] off (unused)
549 * @return number of written bytes on success, negated error code on failure.
551 static ssize_t chlg_write(struct file *file, const char __user *buff,
552 size_t count, loff_t *off)
554 struct chlg_reader_state *crs = file->private_data;
561 if (count > CHLG_CONTROL_CMD_MAX)
564 OBD_ALLOC(kbuf, CHLG_CONTROL_CMD_MAX);
568 if (copy_from_user(kbuf, buff, count))
569 GOTO(out_kbuf, rc = -EFAULT);
571 kbuf[CHLG_CONTROL_CMD_MAX - 1] = '\0';
573 if (sscanf(kbuf, "clear:cl%u:%llu", &reader, &record) == 2)
574 rc = chlg_clear(crs, reader, record);
580 OBD_FREE(kbuf, CHLG_CONTROL_CMD_MAX);
581 return rc < 0 ? rc : count;
585 * Open handler, initialize internal CRS state and spawn prefetch thread if
587 * @param[in] inode Inode struct for the open character device.
588 * @param[in] file Corresponding file pointer.
589 * @return 0 on success, negated error code on failure.
591 static int chlg_open(struct inode *inode, struct file *file)
593 struct chlg_reader_state *crs;
594 struct chlg_registered_dev *dev;
595 struct task_struct *task;
599 dev = container_of(inode->i_cdev, struct chlg_registered_dev, ced_cdev);
605 kref_get(&dev->ced_refs);
607 crs->crs_err = false;
608 crs->crs_eof = false;
610 mutex_init(&crs->crs_lock);
611 INIT_LIST_HEAD(&crs->crs_rec_queue);
612 init_waitqueue_head(&crs->crs_waitq_prod);
613 init_waitqueue_head(&crs->crs_waitq_cons);
615 if (file->f_mode & FMODE_READ) {
616 task = kthread_run(chlg_load, crs, "chlg_load_thread");
619 CERROR("%s: cannot start changelog thread: rc = %d\n",
623 crs->crs_prod_task = task;
626 file->private_data = crs;
630 kref_put(&dev->ced_refs, chlg_dev_clear);
636 * Close handler, release resources.
638 * @param[in] inode Inode struct for the open character device.
639 * @param[in] file Corresponding file pointer.
640 * @return 0 on success, negated error code on failure.
642 static int chlg_release(struct inode *inode, struct file *file)
644 struct chlg_reader_state *crs = file->private_data;
645 struct chlg_rec_entry *rec;
646 struct chlg_rec_entry *tmp;
649 if (crs->crs_prod_task)
650 rc = kthread_stop(crs->crs_prod_task);
652 list_for_each_entry_safe(rec, tmp, &crs->crs_rec_queue, enq_linkage)
653 enq_record_delete(rec);
655 kref_put(&crs->crs_ced->ced_refs, chlg_dev_clear);
662 * Poll handler, indicates whether the device is readable (new records) and
665 * @param[in] file Device file pointer.
666 * @param[in] wait (opaque)
667 * @return combination of the poll status flags.
669 static unsigned int chlg_poll(struct file *file, poll_table *wait)
671 struct chlg_reader_state *crs = file->private_data;
672 unsigned int mask = 0;
674 mutex_lock(&crs->crs_lock);
675 poll_wait(file, &crs->crs_waitq_cons, wait);
676 if (crs->crs_rec_count > 0)
677 mask |= POLLIN | POLLRDNORM;
682 mutex_unlock(&crs->crs_lock);
686 static long chlg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
690 struct chlg_reader_state *crs = file->private_data;
692 case OBD_IOC_CHLG_POLL:
693 crs->crs_poll = !!arg;
703 static const struct file_operations chlg_fops = {
704 .owner = THIS_MODULE,
705 .llseek = chlg_llseek,
709 .release = chlg_release,
711 .unlocked_ioctl = chlg_ioctl,
715 * This uses obd_name of the form: "testfs-MDT0000-mdc-ffff88006501600"
716 * and returns a name of the form: "changelog-testfs-MDT0000".
718 static void get_target_name(char *name, size_t name_len, struct obd_device *obd)
722 snprintf(name, name_len, "%s", obd->obd_name);
724 /* Find the 2nd '-' from the end and truncate on it */
725 for (i = 0; i < 2; i++) {
726 char *p = strrchr(name, '-');
735 * Find a changelog character device by name.
736 * All devices registered during MDC setup are listed in a global list with
737 * their names attached.
739 static struct chlg_registered_dev *
740 chlg_registered_dev_find_by_name(const char *name)
742 struct chlg_registered_dev *dit;
744 LASSERT(mutex_is_locked(&chlg_registered_dev_lock));
745 list_for_each_entry(dit, &chlg_registered_devices, ced_link)
746 if (strcmp(name, dit->ced_name) == 0)
752 * Find chlg_registered_dev structure for a given OBD device.
753 * This is bad O(n^2) but for each filesystem:
754 * - N is # of MDTs times # of mount points
755 * - this only runs at shutdown
757 static struct chlg_registered_dev *
758 chlg_registered_dev_find_by_obd(const struct obd_device *obd)
760 struct chlg_registered_dev *dit;
761 struct obd_device *oit;
763 LASSERT(mutex_is_locked(&chlg_registered_dev_lock));
764 list_for_each_entry(dit, &chlg_registered_devices, ced_link)
765 list_for_each_entry(oit, &dit->ced_obds,
766 u.cli.cl_chg_dev_linkage)
773 * Changelog character device initialization.
774 * Register a misc character device with a dynamic minor number, under a name
775 * of the form: 'changelog-fsname-MDTxxxx'. Reference this OBD device with it.
777 * @param[in] obd This MDC obd_device.
778 * @return 0 on success, negated error code on failure.
780 int mdc_changelog_cdev_init(struct obd_device *obd)
782 struct chlg_registered_dev *exist;
783 struct chlg_registered_dev *entry;
787 OBD_ALLOC_PTR(entry);
791 get_target_name(entry->ced_name, sizeof(entry->ced_name), obd);
793 kref_init(&entry->ced_refs);
794 INIT_LIST_HEAD(&entry->ced_obds);
795 INIT_LIST_HEAD(&entry->ced_link);
797 mutex_lock(&chlg_registered_dev_lock);
798 exist = chlg_registered_dev_find_by_name(entry->ced_name);
800 kref_get(&exist->ced_refs);
801 list_add_tail(&obd->u.cli.cl_chg_dev_linkage, &exist->ced_obds);
802 GOTO(out_unlock, rc = 0);
805 list_add_tail(&obd->u.cli.cl_chg_dev_linkage, &entry->ced_obds);
806 list_add_tail(&entry->ced_link, &chlg_registered_devices);
808 rc = chlg_minor_alloc(&minor);
810 GOTO(out_unlock, rc);
812 device_initialize(&entry->ced_device);
813 entry->ced_device.devt = MKDEV(MAJOR(mdc_changelog_dev), minor);
814 entry->ced_device.class = mdc_changelog_class;
815 entry->ced_device.release = chlg_device_release;
816 dev_set_drvdata(&entry->ced_device, entry);
817 rc = dev_set_name(&entry->ced_device, "%s-%s", MDC_CHANGELOG_DEV_NAME,
822 /* Register new character device */
823 cdev_init(&entry->ced_cdev, &chlg_fops);
824 entry->ced_cdev.owner = THIS_MODULE;
825 rc = cdev_device_add(&entry->ced_cdev, &entry->ced_device);
827 GOTO(out_device_name, rc);
829 entry = NULL; /* prevent it from being freed below */
830 GOTO(out_unlock, rc = 0);
833 kfree_const(entry->ced_device.kobj.name);
836 chlg_minor_free(minor);
838 list_del_init(&obd->u.cli.cl_chg_dev_linkage);
839 list_del(&entry->ced_link);
842 mutex_unlock(&chlg_registered_dev_lock);
849 * Release OBD, decrease reference count of the corresponding changelog device.
851 void mdc_changelog_cdev_finish(struct obd_device *obd)
853 struct chlg_registered_dev *dev;
856 mutex_lock(&chlg_registered_dev_lock);
857 dev = chlg_registered_dev_find_by_obd(obd);
858 list_del_init(&obd->u.cli.cl_chg_dev_linkage);
859 kref_put(&dev->ced_refs, chlg_dev_clear);
860 mutex_unlock(&chlg_registered_dev_lock);