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>
40 #include "mdc_internal.h"
44 * -- Changelog delivery through character device --
48 * Mutex to protect chlg_registered_devices below
50 static DEFINE_MUTEX(chlg_registered_dev_lock);
53 * Global linked list of all registered devices (one per MDT).
55 static LIST_HEAD(chlg_registered_devices);
58 struct chlg_registered_dev {
59 /* Device name of the form "changelog-{MDTNAME}" */
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 */
67 /* Link within the global chlg_registered_devices */
68 struct list_head ced_link;
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 */
78 /* EOF, no more records available */
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 */
90 /* List of prefetched enqueued_record::enq_linkage_items */
91 struct list_head crs_rec_queue;
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 */
99 /* Copy of a changelog record (see struct llog_changelog_rec) */
100 struct changelog_rec enq_record[];
104 /* Number of records to prefetch locally. */
105 CDEV_CHLG_MAX_PREFETCH = 1024,
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.
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
118 * @return 0 or LLOG_PROC_* control code on success, negated error on failure.
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)
124 struct llog_changelog_rec *rec;
125 struct chlg_reader_state *crs = data;
126 struct chlg_rec_entry *enq;
131 LASSERT(crs != NULL);
132 LASSERT(hdr != NULL);
134 rec = container_of(hdr, struct llog_changelog_rec, cr_hdr);
136 if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
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,
141 PFID(lu_object_fid(&llh->lgh_obj->do_lu)), rc);
145 /* Skip undesired records */
146 if (rec->cr.cr_index < crs->crs_start_offset)
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));
156 wait_event_interruptible(crs->crs_waitq_prod,
157 crs->crs_rec_count < CDEV_CHLG_MAX_PREFETCH ||
158 kthread_should_stop());
160 if (kthread_should_stop())
161 RETURN(LLOG_PROC_BREAK);
163 len = changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
164 OBD_ALLOC(enq, sizeof(*enq) + len);
168 INIT_LIST_HEAD(&enq->enq_linkage);
169 enq->enq_length = len;
170 memcpy(enq->enq_record, &rec->cr, len);
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);
177 wake_up_all(&crs->crs_waitq_cons);
183 * Remove record from the list it is attached to and free it.
185 static void enq_record_delete(struct chlg_rec_entry *rec)
187 list_del(&rec->enq_linkage);
188 OBD_FREE(rec, sizeof(*rec) + rec->enq_length);
192 * Record prefetch thread entry point. Opens the changelog catalog and starts
195 * @param[in,out] args chlg_reader_state passed from caller.
196 * @return 0 on success, negated error code on failure.
198 static int chlg_load(void *args)
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;
207 ctx = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
209 GOTO(err_out, rc = -ENOENT);
211 rc = llog_open(NULL, ctx, &llh, NULL, CHANGELOG_CATALOG,
214 CERROR("%s: fail to open changelog catalog: rc = %d\n",
219 rc = llog_init_handle(NULL, llh,
222 LLOG_F_EXT_EXTRA_FLAGS |
223 LLOG_F_EXT_X_UIDGID |
229 CERROR("%s: fail to init llog handle: rc = %d\n",
234 rc = llog_cat_process(NULL, llh, chlg_read_cat_process_cb, crs, 0, 0);
236 CERROR("%s: fail to process llog: rc = %d\n", obd->obd_name, rc);
246 wake_up_all(&crs->crs_waitq_cons);
249 llog_cat_close(NULL, llh);
254 wait_event_interruptible(crs->crs_waitq_prod, kthread_should_stop());
260 * Read handler, dequeues records from the chlg_reader_state if any.
261 * No partial records are copied to userland so this function can return less
262 * data than required (short read).
264 * @param[in] file File pointer to the character device.
265 * @param[out] buff Userland buffer where to copy the records.
266 * @param[in] count Userland buffer size.
267 * @param[out] ppos File position, updated with the index number of the next
269 * @return number of copied bytes on success, negated error code on failure.
271 static ssize_t chlg_read(struct file *file, char __user *buff, size_t count,
274 struct chlg_reader_state *crs = file->private_data;
275 struct chlg_rec_entry *rec;
276 struct chlg_rec_entry *tmp;
277 size_t written_total = 0;
282 if (file->f_flags & O_NONBLOCK && crs->crs_rec_count == 0) {
283 if (crs->crs_err < 0)
284 RETURN(crs->crs_err);
285 else if (crs->crs_eof)
291 rc = wait_event_interruptible(crs->crs_waitq_cons,
292 crs->crs_rec_count > 0 || crs->crs_eof || crs->crs_err);
294 mutex_lock(&crs->crs_lock);
295 list_for_each_entry_safe(rec, tmp, &crs->crs_rec_queue, enq_linkage) {
296 if (written_total + rec->enq_length > count)
299 if (copy_to_user(buff, rec->enq_record, rec->enq_length)) {
304 buff += rec->enq_length;
305 written_total += rec->enq_length;
307 crs->crs_rec_count--;
308 list_move_tail(&rec->enq_linkage, &consumed);
310 crs->crs_start_offset = rec->enq_record->cr_index + 1;
312 mutex_unlock(&crs->crs_lock);
314 if (written_total > 0) {
316 wake_up_all(&crs->crs_waitq_prod);
317 } else if (rc == 0) {
321 list_for_each_entry_safe(rec, tmp, &consumed, enq_linkage)
322 enq_record_delete(rec);
324 *ppos = crs->crs_start_offset;
330 * Jump to a given record index. Helper for chlg_llseek().
332 * @param[in,out] crs Internal reader state.
333 * @param[in] offset Desired offset (index record).
334 * @return 0 on success, negated error code on failure.
336 static int chlg_set_start_offset(struct chlg_reader_state *crs, __u64 offset)
338 struct chlg_rec_entry *rec;
339 struct chlg_rec_entry *tmp;
341 mutex_lock(&crs->crs_lock);
342 if (offset < crs->crs_start_offset) {
343 mutex_unlock(&crs->crs_lock);
347 crs->crs_start_offset = offset;
348 list_for_each_entry_safe(rec, tmp, &crs->crs_rec_queue, enq_linkage) {
349 struct changelog_rec *cr = rec->enq_record;
351 if (cr->cr_index >= crs->crs_start_offset)
354 crs->crs_rec_count--;
355 enq_record_delete(rec);
358 mutex_unlock(&crs->crs_lock);
359 wake_up_all(&crs->crs_waitq_prod);
364 * Move read pointer to a certain record index, encoded as an offset.
366 * @param[in,out] file File pointer to the changelog character device
367 * @param[in] off Offset to skip, actually a record index, not byte count
368 * @param[in] whence Relative/Absolute interpretation of the offset
369 * @return the resulting position on success or negated error code on failure.
371 static loff_t chlg_llseek(struct file *file, loff_t off, int whence)
373 struct chlg_reader_state *crs = file->private_data;
382 pos = file->f_pos + off;
389 /* We cannot go backward */
390 if (pos < file->f_pos)
393 rc = chlg_set_start_offset(crs, pos);
402 * Clear record range for a given changelog reader.
404 * @param[in] crs Current internal state.
405 * @param[in] reader Changelog reader ID (cl1, cl2...)
406 * @param[in] record Record index up which to clear
407 * @return 0 on success, negated error code on failure.
409 static int chlg_clear(struct chlg_reader_state *crs, __u32 reader, __u64 record)
411 struct obd_device *obd = crs->crs_obd;
412 struct changelog_setinfo cs = {
417 return obd_set_info_async(NULL, obd->obd_self_export,
418 strlen(KEY_CHANGELOG_CLEAR),
419 KEY_CHANGELOG_CLEAR, sizeof(cs), &cs, NULL);
422 /** Maximum changelog control command size */
423 #define CHLG_CONTROL_CMD_MAX 64
426 * Handle writes() into the changelog character device. Write() can be used
427 * to request special control operations.
429 * @param[in] file File pointer to the changelog character device
430 * @param[in] buff User supplied data (written data)
431 * @param[in] count Number of written bytes
432 * @param[in] off (unused)
433 * @return number of written bytes on success, negated error code on failure.
435 static ssize_t chlg_write(struct file *file, const char __user *buff,
436 size_t count, loff_t *off)
438 struct chlg_reader_state *crs = file->private_data;
445 if (count > CHLG_CONTROL_CMD_MAX)
448 OBD_ALLOC(kbuf, CHLG_CONTROL_CMD_MAX);
452 if (copy_from_user(kbuf, buff, count))
453 GOTO(out_kbuf, rc = -EFAULT);
455 kbuf[CHLG_CONTROL_CMD_MAX - 1] = '\0';
457 if (sscanf(kbuf, "clear:cl%u:%llu", &reader, &record) == 2)
458 rc = chlg_clear(crs, reader, record);
464 OBD_FREE(kbuf, CHLG_CONTROL_CMD_MAX);
465 return rc < 0 ? rc : count;
469 * Find the OBD device associated to a changelog character device.
470 * @param[in] cdev character device instance descriptor
471 * @return corresponding OBD device or NULL if none was found.
473 static struct obd_device *chlg_obd_get(dev_t cdev)
475 int minor = MINOR(cdev);
476 struct obd_device *obd = NULL;
477 struct chlg_registered_dev *curr;
479 mutex_lock(&chlg_registered_dev_lock);
480 list_for_each_entry(curr, &chlg_registered_devices, ced_link) {
481 if (curr->ced_misc.minor == minor) {
482 /* take the first available OBD device attached */
483 obd = list_first_entry(&curr->ced_obds,
485 u.cli.cl_chg_dev_linkage);
489 mutex_unlock(&chlg_registered_dev_lock);
494 * Open handler, initialize internal CRS state and spawn prefetch thread if
496 * @param[in] inode Inode struct for the open character device.
497 * @param[in] file Corresponding file pointer.
498 * @return 0 on success, negated error code on failure.
500 static int chlg_open(struct inode *inode, struct file *file)
502 struct chlg_reader_state *crs;
503 struct obd_device *obd = chlg_obd_get(inode->i_rdev);
504 struct task_struct *task;
516 crs->crs_err = false;
517 crs->crs_eof = false;
519 mutex_init(&crs->crs_lock);
520 INIT_LIST_HEAD(&crs->crs_rec_queue);
521 init_waitqueue_head(&crs->crs_waitq_prod);
522 init_waitqueue_head(&crs->crs_waitq_cons);
524 if (file->f_mode & FMODE_READ) {
525 task = kthread_run(chlg_load, crs, "chlg_load_thread");
528 CERROR("%s: cannot start changelog thread: rc = %d\n",
532 crs->crs_prod_task = task;
535 file->private_data = crs;
544 * Close handler, release resources.
546 * @param[in] inode Inode struct for the open character device.
547 * @param[in] file Corresponding file pointer.
548 * @return 0 on success, negated error code on failure.
550 static int chlg_release(struct inode *inode, struct file *file)
552 struct chlg_reader_state *crs = file->private_data;
553 struct chlg_rec_entry *rec;
554 struct chlg_rec_entry *tmp;
557 if (crs->crs_prod_task)
558 rc = kthread_stop(crs->crs_prod_task);
560 list_for_each_entry_safe(rec, tmp, &crs->crs_rec_queue, enq_linkage)
561 enq_record_delete(rec);
569 * Poll handler, indicates whether the device is readable (new records) and
572 * @param[in] file Device file pointer.
573 * @param[in] wait (opaque)
574 * @return combination of the poll status flags.
576 static unsigned int chlg_poll(struct file *file, poll_table *wait)
578 struct chlg_reader_state *crs = file->private_data;
579 unsigned int mask = 0;
581 mutex_lock(&crs->crs_lock);
582 poll_wait(file, &crs->crs_waitq_cons, wait);
583 if (crs->crs_rec_count > 0)
584 mask |= POLLIN | POLLRDNORM;
589 mutex_unlock(&crs->crs_lock);
593 static const struct file_operations chlg_fops = {
594 .owner = THIS_MODULE,
595 .llseek = chlg_llseek,
599 .release = chlg_release,
604 * This uses obd_name of the form: "testfs-MDT0000-mdc-ffff88006501600"
605 * and returns a name of the form: "changelog-testfs-MDT0000".
607 static void get_chlg_name(char *name, size_t name_len, struct obd_device *obd)
611 snprintf(name, name_len, "changelog-%s", obd->obd_name);
613 /* Find the 2nd '-' from the end and truncate on it */
614 for (i = 0; i < 2; i++) {
615 char *p = strrchr(name, '-');
624 * Find a changelog character device by name.
625 * All devices registered during MDC setup are listed in a global list with
626 * their names attached.
628 static struct chlg_registered_dev *
629 chlg_registered_dev_find_by_name(const char *name)
631 struct chlg_registered_dev *dit;
633 list_for_each_entry(dit, &chlg_registered_devices, ced_link)
634 if (strcmp(name, dit->ced_name) == 0)
640 * Find chlg_registered_dev structure for a given OBD device.
641 * This is bad O(n^2) but for each filesystem:
642 * - N is # of MDTs times # of mount points
643 * - this only runs at shutdown
645 static struct chlg_registered_dev *
646 chlg_registered_dev_find_by_obd(const struct obd_device *obd)
648 struct chlg_registered_dev *dit;
649 struct obd_device *oit;
651 list_for_each_entry(dit, &chlg_registered_devices, ced_link)
652 list_for_each_entry(oit, &dit->ced_obds,
653 u.cli.cl_chg_dev_linkage)
660 * Changelog character device initialization.
661 * Register a misc character device with a dynamic minor number, under a name
662 * of the form: 'changelog-fsname-MDTxxxx'. Reference this OBD device with it.
664 * @param[in] obd This MDC obd_device.
665 * @return 0 on success, negated error code on failure.
667 int mdc_changelog_cdev_init(struct obd_device *obd)
669 struct chlg_registered_dev *exist;
670 struct chlg_registered_dev *entry;
674 OBD_ALLOC_PTR(entry);
678 get_chlg_name(entry->ced_name, sizeof(entry->ced_name), obd);
680 entry->ced_misc.minor = MISC_DYNAMIC_MINOR;
681 entry->ced_misc.name = entry->ced_name;
682 entry->ced_misc.fops = &chlg_fops;
684 kref_init(&entry->ced_refs);
685 INIT_LIST_HEAD(&entry->ced_obds);
686 INIT_LIST_HEAD(&entry->ced_link);
688 mutex_lock(&chlg_registered_dev_lock);
689 exist = chlg_registered_dev_find_by_name(entry->ced_name);
691 kref_get(&exist->ced_refs);
692 list_add_tail(&obd->u.cli.cl_chg_dev_linkage, &exist->ced_obds);
693 GOTO(out_unlock, rc = 0);
696 /* Register new character device */
697 rc = misc_register(&entry->ced_misc);
699 GOTO(out_unlock, rc);
701 list_add_tail(&obd->u.cli.cl_chg_dev_linkage, &entry->ced_obds);
702 list_add_tail(&entry->ced_link, &chlg_registered_devices);
704 entry = NULL; /* prevent it from being freed below */
707 mutex_unlock(&chlg_registered_dev_lock);
714 * Deregister a changelog character device whose refcount has reached zero.
716 static void chlg_dev_clear(struct kref *kref)
718 struct chlg_registered_dev *entry = container_of(kref,
719 struct chlg_registered_dev,
723 list_del(&entry->ced_link);
724 misc_deregister(&entry->ced_misc);
730 * Release OBD, decrease reference count of the corresponding changelog device.
732 void mdc_changelog_cdev_finish(struct obd_device *obd)
734 struct chlg_registered_dev *dev = chlg_registered_dev_find_by_obd(obd);
737 mutex_lock(&chlg_registered_dev_lock);
738 list_del_init(&obd->u.cli.cl_chg_dev_linkage);
739 kref_put(&dev->ced_refs, chlg_dev_clear);
740 mutex_unlock(&chlg_registered_dev_lock);