+int mds_lock_slave_objs(struct obd_device *obd, struct dentry *dentry,
+ struct lustre_handle **rlockh)
+{
+ struct mds_obd *mds = &obd->u.mds;
+ struct mdc_op_data op_data;
+ struct lookup_intent it;
+ struct mea *mea = NULL;
+ int mea_size, rc;
+
+ LASSERT(rlockh != NULL);
+ LASSERT(dentry != NULL);
+ LASSERT(dentry->d_inode != NULL);
+
+ /* clustered MD ? */
+ if (!mds->mds_lmv_obd)
+ return 0;
+
+ /* a dir can be splitted only */
+ if (!S_ISDIR(dentry->d_inode->i_mode))
+ return 0;
+
+ rc = mds_get_lmv_attr(obd, dentry->d_inode, &mea, &mea_size);
+ if (rc)
+ return rc;
+
+ if (mea == NULL)
+ return 0;
+ if (mea->mea_count == 0) {
+ /* this is slave object */
+ GOTO(cleanup, rc = 0);
+ }
+
+ CDEBUG(D_OTHER, "%s: lock slaves for %lu/%lu\n", obd->obd_name,
+ (unsigned long) dentry->d_inode->i_ino,
+ (unsigned long) dentry->d_inode->i_generation);
+
+ OBD_ALLOC(*rlockh, sizeof(struct lustre_handle) * mea->mea_count);
+ if (*rlockh == NULL)
+ GOTO(cleanup, rc = -ENOMEM);
+ memset(*rlockh, 0, sizeof(struct lustre_handle) * mea->mea_count);
+
+ memset(&op_data, 0, sizeof(op_data));
+ op_data.mea1 = mea;
+ it.it_op = IT_UNLINK;
+ rc = md_enqueue(mds->mds_lmv_exp, LDLM_IBITS, &it, LCK_EX, &op_data,
+ *rlockh, NULL, 0, ldlm_completion_ast, mds_blocking_ast,
+ NULL);
+cleanup:
+ OBD_FREE(mea, mea_size);
+ RETURN(rc);
+}
+
+void mds_unlock_slave_objs(struct obd_device *obd, struct dentry *dentry,
+ struct lustre_handle *lockh)
+{
+ struct mds_obd *mds = &obd->u.mds;
+ struct mea *mea = NULL;
+ int mea_size, rc, i;
+
+ if (lockh == NULL)
+ return;
+
+ LASSERT(mds->mds_lmv_obd != NULL);
+ LASSERT(S_ISDIR(dentry->d_inode->i_mode));
+
+ rc = mds_get_lmv_attr(obd, dentry->d_inode, &mea, &mea_size);
+ if (rc) {
+ CERROR("locks are leaked\n");
+ return;
+ }
+ LASSERT(mea_size != 0);
+ LASSERT(mea != NULL);
+ LASSERT(mea->mea_count != 0);
+
+ CDEBUG(D_OTHER, "%s: unlock slaves for %lu/%lu\n", obd->obd_name,
+ (unsigned long) dentry->d_inode->i_ino,
+ (unsigned long) dentry->d_inode->i_generation);
+
+ for (i = 0; i < mea->mea_count; i++) {
+ if (lockh[i].cookie != 0)
+ ldlm_lock_decref(lockh + i, LCK_EX);
+ }
+
+ OBD_FREE(lockh, sizeof(struct lustre_handle) * mea->mea_count);
+ OBD_FREE(mea, mea_size);
+ return;
+}
+
+int mds_unlink_slave_objs(struct obd_device *obd, struct dentry *dentry)
+{
+ struct mds_obd *mds = &obd->u.mds;
+ struct ptlrpc_request *req = NULL;
+ struct mdc_op_data op_data;
+ struct mea *mea = NULL;
+ int mea_size, rc;
+
+ /* clustered MD ? */
+ if (!mds->mds_lmv_obd)
+ return 0;
+
+ /* a dir can be splitted only */
+ if (!S_ISDIR(dentry->d_inode->i_mode))
+ RETURN(0);
+
+ rc = mds_get_lmv_attr(obd, dentry->d_inode, &mea, &mea_size);
+ if (rc)
+ RETURN(rc);
+
+ if (mea == NULL)
+ return 0;
+ if (mea->mea_count == 0)
+ GOTO(cleanup, rc = 0);
+
+ CDEBUG(D_OTHER, "%s: unlink slaves for %lu/%lu\n", obd->obd_name,
+ (unsigned long) dentry->d_inode->i_ino,
+ (unsigned long) dentry->d_inode->i_generation);
+
+ memset(&op_data, 0, sizeof(op_data));
+ op_data.mea1 = mea;
+ rc = md_unlink(mds->mds_lmv_exp, &op_data, &req);
+ LASSERT(req == NULL);
+cleanup:
+ OBD_FREE(mea, mea_size);
+ RETURN(rc);
+}
+
+struct ide_tracking {
+ int entries;
+ int empty;
+};
+
+int mds_ide_filldir(void *__buf, const char *name, int namelen,
+ loff_t offset, ino_t ino, unsigned int d_type)
+{
+ struct ide_tracking *it = __buf;
+
+ if (ino == 0)
+ return 0;
+
+ it->entries++;
+ if (it->entries > 2)
+ goto noempty;
+ if (namelen > 2)
+ goto noempty;
+ if (name[0] == '.' && namelen == 1)
+ return 0;
+ if (name[0] == '.' && name[1] == '.' && namelen == 2)
+ return 0;
+noempty:
+ it->empty = 0;
+ return -ENOTEMPTY;
+}
+
+int mds_is_dir_empty(struct obd_device *obd, struct dentry *dentry)
+{
+ struct ide_tracking it;
+ struct file * file;
+ char *file_name;
+ int nlen, i, rc;
+
+ it.entries = 0;
+ it.empty = 1;
+
+ nlen = strlen("__iopen__/") + 10 + 1;
+ OBD_ALLOC(file_name, nlen);
+ if (!file_name)
+ RETURN(-ENOMEM);
+ i = sprintf(file_name, "__iopen__/0x%lx", dentry->d_inode->i_ino);
+
+ file = filp_open(file_name, O_RDONLY, 0);
+ if (IS_ERR(file)) {
+ CERROR("can't open directory %s: %d\n",
+ file_name, (int) PTR_ERR(file));
+ GOTO(cleanup, rc = PTR_ERR(file));
+ }
+
+ rc = vfs_readdir(file, mds_ide_filldir, &it);
+ filp_close(file, 0);
+
+ if (it.empty && rc == 0)
+ rc = 1;
+ else
+ rc = 0;
+
+cleanup:
+ OBD_FREE(file_name, nlen);
+ return rc;
+}
+
+int mds_lock_and_check_slave(int offset, struct ptlrpc_request *req,
+ struct lustre_handle *lockh)
+{
+ struct obd_device *obd = req->rq_export->exp_obd;
+ struct dentry *dentry = NULL;
+ struct lvfs_run_ctxt saved;
+ int cleanup_phase = 0;
+ struct mds_req_sec_desc *rsd;
+ struct mds_body *body;
+ struct lvfs_ucred uc;
+ int rc, update_mode;
+ ENTRY;
+
+ rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
+ if (!rsd) {
+ CERROR("Can't unpack security desc\n");
+ GOTO(cleanup, rc = -EFAULT);
+ }
+ mds_squash_root(&obd->u.mds, rsd, &req->rq_peer.peer_id.nid);
+
+ body = lustre_swab_reqbuf(req, offset, sizeof(*body),
+ lustre_swab_mds_body);
+ if (body == NULL) {
+ CERROR("Can't swab mds_body\n");
+ GOTO(cleanup, rc = -EFAULT);
+ }
+ CDEBUG(D_OTHER, "%s: check slave %lu/%lu\n", obd->obd_name,
+ (unsigned long) body->fid1.id,
+ (unsigned long) body->fid1.generation);
+ dentry = mds_fid2locked_dentry(obd, &body->fid1, NULL, LCK_EX, lockh,
+ &update_mode, NULL, 0,
+ MDS_INODELOCK_UPDATE);
+ if (IS_ERR(dentry)) {
+ CERROR("can't find inode: %d\n", (int) PTR_ERR(dentry));
+ GOTO(cleanup, rc = PTR_ERR(dentry));
+ }
+ cleanup_phase = 1;
+
+ LASSERT(S_ISDIR(dentry->d_inode->i_mode));
+
+ rc = mds_init_ucred(&uc, rsd);
+ if (rc) {
+ CERROR("can't init ucred\n");
+ GOTO(cleanup, rc);
+ }
+
+ push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
+
+ rc = 0;
+ if (!mds_is_dir_empty(obd, dentry))
+ rc = -ENOTEMPTY;
+
+cleanup:
+ switch(cleanup_phase) {
+ case 1:
+ if (rc)
+ ldlm_lock_decref(lockh, LCK_EX);
+ l_dput(dentry);
+ pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
+ mds_exit_ucred(&uc);
+ default:
+ break;
+ }
+ RETURN(rc);
+}
+
+int mds_convert_mea_ea(struct obd_device *obd, struct inode *inode,
+ struct lov_mds_md *lmm, int lmmsize)
+{
+ int i, rc, err, size;
+ struct mea_old *old;
+ struct mea *mea;
+ struct mea *new;
+ void *handle;
+ ENTRY;
+
+ mea = (struct mea *) lmm;
+ if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
+ mea->mea_magic == MEA_MAGIC_ALL_CHARS)
+ RETURN(0);
+
+ old = (struct mea_old *) lmm;
+ rc = sizeof(struct ll_fid) * old->mea_count + sizeof(struct mea_old);
+ if (old->mea_count > 256 || old->mea_master > 256 || lmmsize < rc
+ || old->mea_master > old->mea_count) {
+ CWARN("unknown MEA format, dont convert it\n");
+ CWARN(" count %u, master %u, size %u\n",
+ old->mea_count, old->mea_master, rc);
+ RETURN(0);
+ }
+
+ CWARN("converting MEA EA on %lu/%u from V0 to V1 (%u/%u)\n",
+ inode->i_ino, inode->i_generation, old->mea_count, old->mea_master);
+
+ size = sizeof(struct ll_fid) * old->mea_count + sizeof(struct mea);
+ OBD_ALLOC(new, size);
+ if (new == NULL)
+ RETURN(-ENOMEM);
+
+ new->mea_magic = MEA_MAGIC_LAST_CHAR;
+ new->mea_count = old->mea_count;
+ new->mea_master = old->mea_master;
+ for (i = 0; i < new->mea_count; i++)
+ new->mea_fids[i] = old->mea_fids[i];
+
+ handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
+ if (IS_ERR(handle)) {
+ rc = PTR_ERR(handle);
+ GOTO(conv_free, rc);
+ }
+
+ rc = fsfilt_set_md(obd, inode, handle, (struct lov_mds_md *) new, size);
+ if (rc > lmmsize)
+ size = lmmsize;
+ memcpy(lmm, new, size);
+
+ err = fsfilt_commit(obd, obd->u.mds.mds_sb, inode, handle, 0);
+ if (!rc)
+ rc = err ? err : size;
+ GOTO(conv_free, rc);
+conv_free:
+ OBD_FREE(new, size);
+ return rc;
+}
+