SmartRAG: Jointly Learn RAG-Related Tasks From the Environment Feedback