+ if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
+ CERROR("%s: record is too large: %d > %d\n",
+ loghandle2name(llh), rec->lrh_len, chunk_size);
+ return -EINVAL;
+ }
+ if (rec->lrh_index >= LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr)) {
+ CERROR("%s: index is too high: %d\n",
+ loghandle2name(llh), rec->lrh_index);
+ return -EINVAL;
+ }
+ if ((rec->lrh_type & LLOG_OP_MASK) != LLOG_OP_MAGIC) {
+ CERROR("%s: magic %x is bad\n",
+ loghandle2name(llh), rec->lrh_type);
+ return -EINVAL;
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL(llog_verify_record);
+
+static inline bool llog_is_index_skipable(int idx, struct llog_log_hdr *llh,
+ struct llog_process_cat_data *cd)
+{
+ if (cd && (cd->lpcd_read_mode & LLOG_READ_MODE_RAW))
+ return false;
+
+ return !test_bit_le(idx, LLOG_HDR_BITMAP(llh));
+}
+
+static int llog_process_thread(void *arg)
+{
+ struct llog_process_info *lpi = arg;
+ struct llog_handle *loghandle = lpi->lpi_loghandle;
+ struct llog_log_hdr *llh = loghandle->lgh_hdr;
+ struct llog_process_cat_data *cd = lpi->lpi_catdata;
+ struct llog_thread_info *lti;
+ char *buf;
+ size_t chunk_size;
+ __u64 cur_offset;
+ int rc = 0, index = 1, last_index;
+ int saved_index = 0;
+ int last_called_index = 0;
+ bool repeated = false;
+ bool refresh_idx = false;
+
+ ENTRY;
+
+ if (llh == NULL)
+ RETURN(-EINVAL);
+
+ lti = lpi->lpi_env == NULL ? NULL : llog_info(lpi->lpi_env);
+
+ cur_offset = chunk_size = llh->llh_hdr.lrh_len;
+ /* expect chunk_size to be power of two */
+ LASSERT(is_power_of_2(chunk_size));
+
+ OBD_ALLOC_LARGE(buf, chunk_size);
+ if (buf == NULL) {
+ lpi->lpi_rc = -ENOMEM;
+ RETURN(0);
+ }
+
+ if (cd != NULL) {
+ last_called_index = cd->lpcd_first_idx;
+ index = cd->lpcd_first_idx + 1;
+ }
+ if (cd && cd->lpcd_last_idx)
+ last_index = cd->lpcd_last_idx;
+ else if (cd && (cd->lpcd_read_mode & LLOG_READ_MODE_RAW))
+ last_index = loghandle->lgh_last_idx;
+ else
+ last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
+
+ while (rc == 0) {
+ struct llog_rec_hdr *rec;
+ off_t chunk_offset = 0;
+ unsigned int buf_offset = 0;
+ int lh_last_idx;
+ int synced_idx = 0;
+
+ /* skip records not set in bitmap */
+ while (index <= last_index &&
+ llog_is_index_skipable(index, llh, cd))
+ ++index;
+
+ /* There are no indices prior the last_index */
+ if (index > last_index)
+ break;
+
+ CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
+ last_index);
+
+repeat:
+ /* get the buf with our target record; avoid old garbage */
+ memset(buf, 0, chunk_size);
+ /* the record index for outdated chunk data */
+ /* it is safe to process buffer until saved lgh_last_idx */
+ lh_last_idx = LLOG_HDR_TAIL(llh)->lrt_index;
+ rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
+ index, &cur_offset, buf, chunk_size);
+ if (repeated && rc)
+ CDEBUG(D_OTHER, "cur_offset %llu, chunk_offset %llu,"
+ " buf_offset %u, rc = %d\n", cur_offset,
+ (__u64)chunk_offset, buf_offset, rc);
+ if (rc == -ESTALE)
+ GOTO(out, rc = 0);
+ /* we`ve tried to reread the chunk, but there is no
+ * new records */
+ if (rc == -EIO && repeated && (chunk_offset + buf_offset) ==
+ cur_offset)
+ GOTO(out, rc = 0);
+ if (rc != 0)
+ GOTO(out, rc);
+
+ /* NB: after llog_next_block() call the cur_offset is the
+ * offset of the next block after read one.
+ * The absolute offset of the current chunk is calculated
+ * from cur_offset value and stored in chunk_offset variable.
+ */
+ if ((cur_offset & (chunk_size - 1)) != 0)
+ chunk_offset = cur_offset & ~(chunk_size - 1);
+ else
+ chunk_offset = cur_offset - chunk_size;
+
+ /* NB: when rec->lrh_len is accessed it is already swabbed
+ * since it is used at the "end" of the loop and the rec
+ * swabbing is done at the beginning of the loop. */
+ for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
+ (char *)rec < buf + chunk_size;
+ rec = llog_rec_hdr_next(rec)) {
+
+ CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
+ rec, rec->lrh_type);
+
+ if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
+ lustre_swab_llog_rec(rec);
+
+ CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
+ rec->lrh_type, rec->lrh_index);
+
+ if (index == (synced_idx + 1) &&
+ synced_idx == LLOG_HDR_TAIL(llh)->lrt_index)
+ GOTO(out, rc = 0);
+
+ if (OBD_FAIL_PRECHECK(OBD_FAIL_LLOG_PROCESS_TIMEOUT) &&
+ cfs_fail_val == (unsigned int)
+ (loghandle->lgh_id.lgl_oi.oi.oi_id &
+ 0xFFFFFFFF)) {
+ OBD_RACE(OBD_FAIL_LLOG_PROCESS_TIMEOUT);
+ }
+
+ /* the bitmap could be changed during processing
+ * records from the chunk. For wrapped catalog
+ * it means we can read deleted record and try to
+ * process it. Check this case and reread the chunk.
+ * It is safe to process to lh_last_idx, including
+ * lh_last_idx if it was synced. We can not do <=
+ * comparison, cause for wrapped catalog lgh_last_idx
+ * could be less than index. So we detect last index
+ * for processing as index == lh_last_idx+1. But when
+ * catalog is wrapped and full lgh_last_idx=llh_cat_idx,
+ * the first processing index is llh_cat_idx+1.The
+ * exception is !(lgh_last_idx == llh_cat_idx &&
+ * index == llh_cat_idx + 1), and after simplification
+ * it turns to
+ * lh_last_idx != LLOG_HDR_TAIL(llh)->lrt_index
+ * This exception is working for catalog only.
+ */
+
+ if ((index == lh_last_idx && synced_idx != index) ||
+ (index == (lh_last_idx + 1) &&
+ lh_last_idx != LLOG_HDR_TAIL(llh)->lrt_index) ||
+ (rec->lrh_index == 0 && !repeated)) {
+
+ /* save offset inside buffer for the re-read */
+ buf_offset = (char *)rec - (char *)buf;
+ cur_offset = chunk_offset;
+ repeated = true;
+ /* We need to be sure lgh_last_idx
+ * record was saved to disk
+ */
+ down_read(&loghandle->lgh_last_sem);
+ synced_idx = LLOG_HDR_TAIL(llh)->lrt_index;
+ up_read(&loghandle->lgh_last_sem);
+ CDEBUG(D_OTHER, "synced_idx: %d\n", synced_idx);
+ goto repeat;
+
+ }
+
+ repeated = false;
+
+ rc = llog_verify_record(loghandle, rec);
+ if (rc) {
+ CERROR("%s: invalid record in llog "DFID
+ " record for index %d/%d: rc = %d\n",
+ loghandle2name(loghandle),
+ PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
+ rec->lrh_index, index, rc);
+ /*
+ * the block seem to be corrupted, let's try
+ * with the next one. reset rc to go to the
+ * next chunk.
+ */
+ refresh_idx = true;
+ index = 0;
+ GOTO(repeat, rc = 0);
+ }
+
+ if (rec->lrh_index < index) {
+ CDEBUG(D_OTHER, "skipping lrh_index %d\n",
+ rec->lrh_index);
+ continue;
+ }
+
+ if (rec->lrh_index > index) {
+ /* the record itself looks good, but we met a
+ * gap which can be result of old bugs, just
+ * keep going */
+ CERROR("%s: "DFID" index %u, expected %u\n",
+ loghandle2name(loghandle),
+ PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
+ rec->lrh_index, index);
+ index = rec->lrh_index;
+ }
+
+ CDEBUG(D_OTHER,
+ "lrh_index: %d lrh_len: %d (%d remains)\n",
+ rec->lrh_index, rec->lrh_len,
+ (int)(buf + chunk_size - (char *)rec));
+
+ /* lgh_cur_offset is used only at llog_test_3 */
+ loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
+ chunk_offset;
+
+ /* if needed, process the callback on this record */
+ if (!llog_is_index_skipable(index, llh, cd)) {
+ struct llog_cookie *lgc;
+ __u64 tmp_off;
+ int tmp_idx;
+
+ CDEBUG((llh->llh_flags & LLOG_F_IS_CAT ?
+ D_HA : D_OTHER),
+ "index: %d, lh_last_idx: %d "
+ "synced_idx: %d lgh_last_idx: %d\n",
+ index, lh_last_idx, synced_idx,
+ loghandle->lgh_last_idx);
+
+ if (lti != NULL) {
+ lgc = <i->lgi_cookie;
+ /* store lu_env for recursive calls */
+ tmp_off = lgc->lgc_offset;
+ tmp_idx = lgc->lgc_index;
+
+ lgc->lgc_offset = (char *)rec -
+ (char *)buf + chunk_offset;
+ lgc->lgc_index = rec->lrh_index;
+ }
+ /* using lu_env for passing record offset to
+ * llog_write through various callbacks */
+ rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
+ lpi->lpi_cbdata);
+ last_called_index = index;
+
+ if (lti != NULL) {
+ lgc->lgc_offset = tmp_off;
+ lgc->lgc_index = tmp_idx;
+ }
+
+ if (rc == LLOG_PROC_BREAK ||
+ rc == LLOG_SKIP_PLAIN) {
+ GOTO(out, rc);
+ } else if (rc == LLOG_DEL_RECORD) {
+ rc = llog_cancel_rec(lpi->lpi_env,
+ loghandle,
+ rec->lrh_index);
+ /* Allow parallel cancelling, ENOENT
+ * means record was canceled at another
+ * processing thread or callback
+ */
+ if (rc == -ENOENT)
+ rc = 0;
+ }
+ if (rc)
+ GOTO(out, rc);
+ /* some stupid callbacks directly cancel records
+ * and delete llog. Check it and stop
+ * processing. */
+ if (loghandle->lgh_hdr == NULL ||
+ loghandle->lgh_hdr->llh_count == 1)
+ GOTO(out, rc = 0);
+ }
+ /* exit if the last index is reached */
+ if (index >= last_index)
+ GOTO(out, rc = 0);
+ ++index;
+ }
+ }
+
+out:
+ CDEBUG(D_HA, "stop processing %s "DOSTID":%x index %d count %d\n",
+ ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" : "plain"),
+ POSTID(&loghandle->lgh_id.lgl_oi), loghandle->lgh_id.lgl_ogen,
+ index, llh->llh_count);
+
+ if (cd != NULL)
+ cd->lpcd_last_idx = last_called_index;
+
+ if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
+ if (dt_object_remote(loghandle->lgh_obj)) {
+ /* If it is remote object, then -EIO might means
+ * disconnection or eviction, let's return -EAGAIN,
+ * so for update recovery log processing, it will
+ * retry until the umount or abort recovery, see
+ * lod_sub_recovery_thread() */
+ CERROR("%s retry remote llog process\n",
+ loghandle2name(loghandle));
+ rc = -EAGAIN;
+ } else {
+ /* something bad happened to the processing of a local
+ * llog file, probably I/O error or the log got
+ * corrupted to be able to finally release the log we
+ * discard any remaining bits in the header */
+ CERROR("%s: Local llog found corrupted #"DOSTID":%x"
+ " %s index %d count %d\n",
+ loghandle2name(loghandle),
+ POSTID(&loghandle->lgh_id.lgl_oi),
+ loghandle->lgh_id.lgl_ogen,
+ ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" :
+ "plain"), index, llh->llh_count);
+
+ while (index <= last_index) {
+ if (test_bit_le(index,
+ LLOG_HDR_BITMAP(llh)) != 0)
+ llog_cancel_rec(lpi->lpi_env, loghandle,
+ index);
+ index++;
+ }
+ rc = 0;
+ }
+ }
+
+ OBD_FREE_LARGE(buf, chunk_size);
+ lpi->lpi_rc = rc;
+ return 0;
+}
+
+static int llog_process_thread_daemonize(void *arg)
+{
+ struct llog_process_info *lpi = arg;
+ struct lu_env env;
+ int rc;
+ struct nsproxy *new_ns, *curr_ns = current->nsproxy;
+
+ task_lock(lpi->lpi_reftask);
+ new_ns = lpi->lpi_reftask->nsproxy;
+ if (curr_ns != new_ns) {
+ get_nsproxy(new_ns);
+
+ current->nsproxy = new_ns;
+ /* XXX: we should call put_nsproxy() instead of
+ * atomic_dec(&ns->count) directly. But put_nsproxy() cannot be
+ * used outside of the kernel itself, because it calls
+ * free_nsproxy() which is not exported by the kernel
+ * (defined in kernel/nsproxy.c) */
+ if (curr_ns)
+ atomic_dec(&curr_ns->count);
+ }
+ task_unlock(lpi->lpi_reftask);
+
+ /* client env has no keys, tags is just 0 */
+ rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
+ if (rc)
+ goto out;
+ lpi->lpi_env = &env;
+
+ rc = llog_process_thread(arg);
+
+ lu_env_fini(&env);
+out:
+ complete(&lpi->lpi_completion);
+ return rc;
+}
+
+int llog_process_or_fork(const struct lu_env *env,
+ struct llog_handle *loghandle,
+ llog_cb_t cb, void *data, void *catdata, bool fork)
+{
+ struct llog_process_info *lpi;
+ struct llog_process_data *d = data;
+ struct llog_process_cat_data *cd = catdata;
+ __u32 flags = loghandle->lgh_hdr->llh_flags;
+ int rc;
+
+ ENTRY;
+
+ OBD_ALLOC_PTR(lpi);
+ if (lpi == NULL) {
+ CERROR("cannot alloc pointer\n");
+ RETURN(-ENOMEM);
+ }
+ lpi->lpi_loghandle = loghandle;
+ lpi->lpi_cb = cb;
+ lpi->lpi_cbdata = data;
+ lpi->lpi_catdata = catdata;
+
+ CDEBUG(D_OTHER, "Processing "DFID" flags 0x%03x startcat %d startidx %d first_idx %d last_idx %d read_mode %d\n",
+ PFID(&loghandle->lgh_id.lgl_oi.oi_fid), flags,
+ (flags & LLOG_F_IS_CAT) && d ? d->lpd_startcat : -1,
+ (flags & LLOG_F_IS_CAT) && d ? d->lpd_startidx : -1,
+ cd ? cd->lpcd_first_idx : -1, cd ? cd->lpcd_last_idx : -1,
+ cd ? cd->lpcd_read_mode : -1);
+ if (fork) {
+ struct task_struct *task;
+
+ /* The new thread can't use parent env,
+ * init the new one in llog_process_thread_daemonize. */
+ lpi->lpi_env = NULL;
+ init_completion(&lpi->lpi_completion);
+ /* take reference to current, so that
+ * llog_process_thread_daemonize() can use it to switch to
+ * namespace associated with current */
+ lpi->lpi_reftask = current;
+ task = kthread_run(llog_process_thread_daemonize, lpi,
+ "llog_process_thread");
+ if (IS_ERR(task)) {
+ rc = PTR_ERR(task);
+ CERROR("%s: cannot start thread: rc = %d\n",
+ loghandle2name(loghandle), rc);
+ GOTO(out_lpi, rc);
+ }
+ wait_for_completion(&lpi->lpi_completion);
+ } else {
+ lpi->lpi_env = env;
+ llog_process_thread(lpi);
+ }
+ rc = lpi->lpi_rc;
+
+out_lpi:
+ OBD_FREE_PTR(lpi);
+ RETURN(rc);
+}
+EXPORT_SYMBOL(llog_process_or_fork);
+
+int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
+ llog_cb_t cb, void *data, void *catdata)
+{
+ int rc;
+ rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
+ return rc == LLOG_DEL_PLAIN ? 0 : rc;
+}
+EXPORT_SYMBOL(llog_process);
+
+static inline const struct cred *llog_raise_resource(void)
+{
+ struct cred *cred = NULL;
+
+ if (cap_raised(current_cap(), CAP_SYS_RESOURCE))
+ return cred;
+
+ cred = prepare_creds();
+ if (!cred)
+ return cred;
+
+ cap_raise(cred->cap_effective, CAP_SYS_RESOURCE);
+ return override_creds(cred);