Khanqahs are like the rahba (courtyard) that Umar (may Allah be pleased with him) built to the side of the Masjid and said: ‘Whoever wants to chat or recite poetry or raise his voice, let him go out to this courtyard.
– Shaykh Muhammad ibn Ali al-Sanusi, al-Salsabil al-mu’in.
The hadith and its explanation.
Imam Malik narrated in the Muwatta that he was told that Umar ibn al-Khattab built a rahba (courtyard) to the side of the Masjid and called it al-Butayha’. He said: ‘Whoever wants to chat (yalghat) or recite poetry or raise his voice, let him go out to this rahba.’
Ibn Abd al-Barr in al-Istidhkar mentioned that several people narrated it from Malik with its full chain of transmission back to Abdullah Ibn Umar, to Umar ibn al-Khattab, may Allah be pleased with them. [See al-Istidhkar]
Raḥba means a big empty space. Al-Tayyibi said: The rahba of a masjid is its courtyard.
Our chief Umar called this courtyard al-Butayha‘. Mulla Ali al-Qari said: Perhaps that is because he covered its floor with batha’ (sand).
Yalghat means: to make noise. Al-Tayyibi said: It means to speak about things that do not concern oneself.
To raise his voice: Mulla Ali Qari said: Even if with dhikr.
[See Mirqat al-Mafatih]
Al-Baji said: When Umar saw how much people sat together and talked in the masjid, and that it sometimes led them to loud chatter, and sometimes they would recite poetry, he built the Butayha’ so that the Masjid becomes only for the remembrance of Allah. He did not mean that all of that is forbidden in the Masjid, but it is only to exalt the Masjid above that, especially the Masjid of Rasool Allah (peace and blessings be upon him). [Al-Zurqani’s commentary on the Muwatta.]
Those who lower their voice in the presence of the Messenger of Allah are those whose hearts Allah has proved to have taqwa. They shall receive forgiveness and a great wage. (49:4)
As al-Baji and Ibn Abd al-Barr and others said, the recitation of poetry is not at all forbidden in masjids, because of the hadith in Sahih Bukhari, narrated by Abu Hurayra:
Sa’id ibn al-Musayyab said, “‘Umar passed by in the mosque while Hassan was reciting poetry. He said, “I used to recite in it when there was someone better than you in it!’ Then he turned to Abu Hurayra and said, “‘I ask you by Allah, did you hear the Messenger of Allah (may Allah bless him and grant him peace) say, “Hassan, reply in defense of me. O Allah, help him by means of the Pure Spirit!”‘ Abu Hurayra said, ‘Yes.'” (Narrated by al-Bukhari).
Conclusion: it is permissible to recite poetry in the masjid, as was done in the presence of the Messenger of Allah, (peace and blessings be upon him). However, Umar thought it would be better to build a space outside for people to use if they want to raise their voice in dhikr, recite poetry, or talk to each other. This space, when dedicated to congregational dhikr, would later become known in Arabic as a zawiya (literally: a corner), or in Egypt a saha (also meaning courtyard), and in Persian as a khanqah. Sometimes it would be an independent building on its own. It is therefore a Sunna established by Umar ibn al-Khattab (may Allah be pleased with him).