mqueue.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  1. /*
  2. * POSIX message queues filesystem for Linux.
  3. *
  4. * Copyright (C) 2003,2004 Krzysztof Benedyczak (golbi@mat.uni.torun.pl)
  5. * Michal Wronski (michal.wronski@gmail.com)
  6. *
  7. * Spinlocks: Mohamed Abbas (abbas.mohamed@intel.com)
  8. * Lockless receive & send, fd based notify:
  9. * Manfred Spraul (manfred@colorfullife.com)
  10. *
  11. * Audit: George Wilson (ltcgcw@us.ibm.com)
  12. *
  13. * This file is released under the GPL.
  14. */
  15. #include <linux/capability.h>
  16. #include <linux/init.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/file.h>
  19. #include <linux/mount.h>
  20. #include <linux/namei.h>
  21. #include <linux/sysctl.h>
  22. #include <linux/poll.h>
  23. #include <linux/mqueue.h>
  24. #include <linux/msg.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/netlink.h>
  27. #include <linux/syscalls.h>
  28. #include <linux/audit.h>
  29. #include <linux/signal.h>
  30. #include <linux/mutex.h>
  31. #include <linux/nsproxy.h>
  32. #include <linux/pid.h>
  33. #include <linux/ipc_namespace.h>
  34. #include <linux/user_namespace.h>
  35. #include <linux/slab.h>
  36. #include <net/sock.h>
  37. #include "util.h"
  38. #define MQUEUE_MAGIC 0x19800202
  39. #define DIRENT_SIZE 20
  40. #define FILENT_SIZE 80
  41. #define SEND 0
  42. #define RECV 1
  43. #define STATE_NONE 0
  44. #define STATE_PENDING 1
  45. #define STATE_READY 2
  46. struct ext_wait_queue { /* queue of sleeping tasks */
  47. struct task_struct *task;
  48. struct list_head list;
  49. struct msg_msg *msg; /* ptr of loaded message */
  50. int state; /* one of STATE_* values */
  51. };
  52. struct mqueue_inode_info {
  53. spinlock_t lock;
  54. struct inode vfs_inode;
  55. wait_queue_head_t wait_q;
  56. struct msg_msg **messages;
  57. struct mq_attr attr;
  58. struct sigevent notify;
  59. struct pid* notify_owner;
  60. struct user_struct *user; /* user who created, for accounting */
  61. struct sock *notify_sock;
  62. struct sk_buff *notify_cookie;
  63. /* for tasks waiting for free space and messages, respectively */
  64. struct ext_wait_queue e_wait_q[2];
  65. unsigned long qsize; /* size of queue in memory (sum of all msgs) */
  66. };
  67. static const struct inode_operations mqueue_dir_inode_operations;
  68. static const struct file_operations mqueue_file_operations;
  69. static const struct super_operations mqueue_super_ops;
  70. static void remove_notification(struct mqueue_inode_info *info);
  71. static struct kmem_cache *mqueue_inode_cachep;
  72. static struct ctl_table_header * mq_sysctl_table;
  73. static inline struct mqueue_inode_info *MQUEUE_I(struct inode *inode)
  74. {
  75. return container_of(inode, struct mqueue_inode_info, vfs_inode);
  76. }
  77. /*
  78. * This routine should be called with the mq_lock held.
  79. */
  80. static inline struct ipc_namespace *__get_ns_from_inode(struct inode *inode)
  81. {
  82. return get_ipc_ns(inode->i_sb->s_fs_info);
  83. }
  84. static struct ipc_namespace *get_ns_from_inode(struct inode *inode)
  85. {
  86. struct ipc_namespace *ns;
  87. spin_lock(&mq_lock);
  88. ns = __get_ns_from_inode(inode);
  89. spin_unlock(&mq_lock);
  90. return ns;
  91. }
  92. static struct inode *mqueue_get_inode(struct super_block *sb,
  93. struct ipc_namespace *ipc_ns, umode_t mode,
  94. struct mq_attr *attr)
  95. {
  96. struct user_struct *u = current_user();
  97. struct inode *inode;
  98. int ret = -ENOMEM;
  99. inode = new_inode(sb);
  100. if (!inode)
  101. goto err;
  102. inode->i_ino = get_next_ino();
  103. inode->i_mode = mode;
  104. inode->i_uid = current_fsuid();
  105. inode->i_gid = current_fsgid();
  106. inode->i_mtime = inode->i_ctime = inode->i_atime = CURRENT_TIME;
  107. if (S_ISREG(mode)) {
  108. struct mqueue_inode_info *info;
  109. struct task_struct *p = current;
  110. unsigned long mq_bytes, mq_msg_tblsz;
  111. inode->i_fop = &mqueue_file_operations;
  112. inode->i_size = FILENT_SIZE;
  113. /* mqueue specific info */
  114. info = MQUEUE_I(inode);
  115. spin_lock_init(&info->lock);
  116. init_waitqueue_head(&info->wait_q);
  117. INIT_LIST_HEAD(&info->e_wait_q[0].list);
  118. INIT_LIST_HEAD(&info->e_wait_q[1].list);
  119. info->notify_owner = NULL;
  120. info->qsize = 0;
  121. info->user = NULL; /* set when all is ok */
  122. memset(&info->attr, 0, sizeof(info->attr));
  123. info->attr.mq_maxmsg = ipc_ns->mq_msg_max;
  124. info->attr.mq_msgsize = ipc_ns->mq_msgsize_max;
  125. if (attr) {
  126. info->attr.mq_maxmsg = attr->mq_maxmsg;
  127. info->attr.mq_msgsize = attr->mq_msgsize;
  128. }
  129. mq_msg_tblsz = info->attr.mq_maxmsg * sizeof(struct msg_msg *);
  130. info->messages = kmalloc(mq_msg_tblsz, GFP_KERNEL);
  131. if (!info->messages)
  132. goto out_inode;
  133. mq_bytes = (mq_msg_tblsz +
  134. (info->attr.mq_maxmsg * info->attr.mq_msgsize));
  135. spin_lock(&mq_lock);
  136. if (u->mq_bytes + mq_bytes < u->mq_bytes ||
  137. u->mq_bytes + mq_bytes > task_rlimit(p, RLIMIT_MSGQUEUE)) {
  138. spin_unlock(&mq_lock);
  139. /* mqueue_evict_inode() releases info->messages */
  140. ret = -EMFILE;
  141. goto out_inode;
  142. }
  143. u->mq_bytes += mq_bytes;
  144. spin_unlock(&mq_lock);
  145. /* all is ok */
  146. info->user = get_uid(u);
  147. } else if (S_ISDIR(mode)) {
  148. inc_nlink(inode);
  149. /* Some things misbehave if size == 0 on a directory */
  150. inode->i_size = 2 * DIRENT_SIZE;
  151. inode->i_op = &mqueue_dir_inode_operations;
  152. inode->i_fop = &simple_dir_operations;
  153. }
  154. return inode;
  155. out_inode:
  156. iput(inode);
  157. err:
  158. return ERR_PTR(ret);
  159. }
  160. static int mqueue_fill_super(struct super_block *sb, void *data, int silent)
  161. {
  162. struct inode *inode;
  163. struct ipc_namespace *ns = data;
  164. int error;
  165. sb->s_blocksize = PAGE_CACHE_SIZE;
  166. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  167. sb->s_magic = MQUEUE_MAGIC;
  168. sb->s_op = &mqueue_super_ops;
  169. inode = mqueue_get_inode(sb, ns, S_IFDIR | S_ISVTX | S_IRWXUGO,
  170. NULL);
  171. if (IS_ERR(inode)) {
  172. error = PTR_ERR(inode);
  173. goto out;
  174. }
  175. sb->s_root = d_alloc_root(inode);
  176. if (!sb->s_root) {
  177. iput(inode);
  178. error = -ENOMEM;
  179. goto out;
  180. }
  181. error = 0;
  182. out:
  183. return error;
  184. }
  185. static struct dentry *mqueue_mount(struct file_system_type *fs_type,
  186. int flags, const char *dev_name,
  187. void *data)
  188. {
  189. if (!(flags & MS_KERNMOUNT))
  190. data = current->nsproxy->ipc_ns;
  191. return mount_ns(fs_type, flags, data, mqueue_fill_super);
  192. }
  193. static void init_once(void *foo)
  194. {
  195. struct mqueue_inode_info *p = (struct mqueue_inode_info *) foo;
  196. inode_init_once(&p->vfs_inode);
  197. }
  198. static struct inode *mqueue_alloc_inode(struct super_block *sb)
  199. {
  200. struct mqueue_inode_info *ei;
  201. ei = kmem_cache_alloc(mqueue_inode_cachep, GFP_KERNEL);
  202. if (!ei)
  203. return NULL;
  204. return &ei->vfs_inode;
  205. }
  206. static void mqueue_i_callback(struct rcu_head *head)
  207. {
  208. struct inode *inode = container_of(head, struct inode, i_rcu);
  209. kmem_cache_free(mqueue_inode_cachep, MQUEUE_I(inode));
  210. }
  211. static void mqueue_destroy_inode(struct inode *inode)
  212. {
  213. call_rcu(&inode->i_rcu, mqueue_i_callback);
  214. }
  215. static void mqueue_evict_inode(struct inode *inode)
  216. {
  217. struct mqueue_inode_info *info;
  218. struct user_struct *user;
  219. unsigned long mq_bytes;
  220. int i;
  221. struct ipc_namespace *ipc_ns;
  222. end_writeback(inode);
  223. if (S_ISDIR(inode->i_mode))
  224. return;
  225. ipc_ns = get_ns_from_inode(inode);
  226. info = MQUEUE_I(inode);
  227. spin_lock(&info->lock);
  228. for (i = 0; i < info->attr.mq_curmsgs; i++)
  229. free_msg(info->messages[i]);
  230. kfree(info->messages);
  231. spin_unlock(&info->lock);
  232. /* Total amount of bytes accounted for the mqueue */
  233. mq_bytes = info->attr.mq_maxmsg * (sizeof(struct msg_msg *)
  234. + info->attr.mq_msgsize);
  235. user = info->user;
  236. if (user) {
  237. spin_lock(&mq_lock);
  238. user->mq_bytes -= mq_bytes;
  239. /*
  240. * get_ns_from_inode() ensures that the
  241. * (ipc_ns = sb->s_fs_info) is either a valid ipc_ns
  242. * to which we now hold a reference, or it is NULL.
  243. * We can't put it here under mq_lock, though.
  244. */
  245. if (ipc_ns)
  246. ipc_ns->mq_queues_count--;
  247. spin_unlock(&mq_lock);
  248. free_uid(user);
  249. }
  250. if (ipc_ns)
  251. put_ipc_ns(ipc_ns);
  252. }
  253. static int mqueue_create(struct inode *dir, struct dentry *dentry,
  254. umode_t mode, struct nameidata *nd)
  255. {
  256. struct inode *inode;
  257. struct mq_attr *attr = dentry->d_fsdata;
  258. int error;
  259. struct ipc_namespace *ipc_ns;
  260. spin_lock(&mq_lock);
  261. ipc_ns = __get_ns_from_inode(dir);
  262. if (!ipc_ns) {
  263. error = -EACCES;
  264. goto out_unlock;
  265. }
  266. if (ipc_ns->mq_queues_count >= ipc_ns->mq_queues_max &&
  267. !capable(CAP_SYS_RESOURCE)) {
  268. error = -ENOSPC;
  269. goto out_unlock;
  270. }
  271. ipc_ns->mq_queues_count++;
  272. spin_unlock(&mq_lock);
  273. inode = mqueue_get_inode(dir->i_sb, ipc_ns, mode, attr);
  274. if (IS_ERR(inode)) {
  275. error = PTR_ERR(inode);
  276. spin_lock(&mq_lock);
  277. ipc_ns->mq_queues_count--;
  278. goto out_unlock;
  279. }
  280. put_ipc_ns(ipc_ns);
  281. dir->i_size += DIRENT_SIZE;
  282. dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
  283. d_instantiate(dentry, inode);
  284. dget(dentry);
  285. return 0;
  286. out_unlock:
  287. spin_unlock(&mq_lock);
  288. if (ipc_ns)
  289. put_ipc_ns(ipc_ns);
  290. return error;
  291. }
  292. static int mqueue_unlink(struct inode *dir, struct dentry *dentry)
  293. {
  294. struct inode *inode = dentry->d_inode;
  295. dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
  296. dir->i_size -= DIRENT_SIZE;
  297. drop_nlink(inode);
  298. dput(dentry);
  299. return 0;
  300. }
  301. /*
  302. * This is routine for system read from queue file.
  303. * To avoid mess with doing here some sort of mq_receive we allow
  304. * to read only queue size & notification info (the only values
  305. * that are interesting from user point of view and aren't accessible
  306. * through std routines)
  307. */
  308. static ssize_t mqueue_read_file(struct file *filp, char __user *u_data,
  309. size_t count, loff_t *off)
  310. {
  311. struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
  312. char buffer[FILENT_SIZE];
  313. ssize_t ret;
  314. spin_lock(&info->lock);
  315. snprintf(buffer, sizeof(buffer),
  316. "QSIZE:%-10lu NOTIFY:%-5d SIGNO:%-5d NOTIFY_PID:%-6d\n",
  317. info->qsize,
  318. info->notify_owner ? info->notify.sigev_notify : 0,
  319. (info->notify_owner &&
  320. info->notify.sigev_notify == SIGEV_SIGNAL) ?
  321. info->notify.sigev_signo : 0,
  322. pid_vnr(info->notify_owner));
  323. spin_unlock(&info->lock);
  324. buffer[sizeof(buffer)-1] = '\0';
  325. ret = simple_read_from_buffer(u_data, count, off, buffer,
  326. strlen(buffer));
  327. if (ret <= 0)
  328. return ret;
  329. filp->f_path.dentry->d_inode->i_atime = filp->f_path.dentry->d_inode->i_ctime = CURRENT_TIME;
  330. return ret;
  331. }
  332. static int mqueue_flush_file(struct file *filp, fl_owner_t id)
  333. {
  334. struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
  335. spin_lock(&info->lock);
  336. if (task_tgid(current) == info->notify_owner)
  337. remove_notification(info);
  338. spin_unlock(&info->lock);
  339. return 0;
  340. }
  341. static unsigned int mqueue_poll_file(struct file *filp, struct poll_table_struct *poll_tab)
  342. {
  343. struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
  344. int retval = 0;
  345. poll_wait(filp, &info->wait_q, poll_tab);
  346. spin_lock(&info->lock);
  347. if (info->attr.mq_curmsgs)
  348. retval = POLLIN | POLLRDNORM;
  349. if (info->attr.mq_curmsgs < info->attr.mq_maxmsg)
  350. retval |= POLLOUT | POLLWRNORM;
  351. spin_unlock(&info->lock);
  352. return retval;
  353. }
  354. /* Adds current to info->e_wait_q[sr] before element with smaller prio */
  355. static void wq_add(struct mqueue_inode_info *info, int sr,
  356. struct ext_wait_queue *ewp)
  357. {
  358. struct ext_wait_queue *walk;
  359. ewp->task = current;
  360. list_for_each_entry(walk, &info->e_wait_q[sr].list, list) {
  361. if (walk->task->static_prio <= current->static_prio) {
  362. list_add_tail(&ewp->list, &walk->list);
  363. return;
  364. }
  365. }
  366. list_add_tail(&ewp->list, &info->e_wait_q[sr].list);
  367. }
  368. /*
  369. * Puts current task to sleep. Caller must hold queue lock. After return
  370. * lock isn't held.
  371. * sr: SEND or RECV
  372. */
  373. static int wq_sleep(struct mqueue_inode_info *info, int sr,
  374. ktime_t *timeout, struct ext_wait_queue *ewp)
  375. {
  376. int retval;
  377. signed long time;
  378. wq_add(info, sr, ewp);
  379. for (;;) {
  380. set_current_state(TASK_INTERRUPTIBLE);
  381. spin_unlock(&info->lock);
  382. time = schedule_hrtimeout_range_clock(timeout, 0,
  383. HRTIMER_MODE_ABS, CLOCK_REALTIME);
  384. while (ewp->state == STATE_PENDING)
  385. cpu_relax();
  386. if (ewp->state == STATE_READY) {
  387. retval = 0;
  388. goto out;
  389. }
  390. spin_lock(&info->lock);
  391. if (ewp->state == STATE_READY) {
  392. retval = 0;
  393. goto out_unlock;
  394. }
  395. if (signal_pending(current)) {
  396. retval = -ERESTARTSYS;
  397. break;
  398. }
  399. if (time == 0) {
  400. retval = -ETIMEDOUT;
  401. break;
  402. }
  403. }
  404. list_del(&ewp->list);
  405. out_unlock:
  406. spin_unlock(&info->lock);
  407. out:
  408. return retval;
  409. }
  410. /*
  411. * Returns waiting task that should be serviced first or NULL if none exists
  412. */
  413. static struct ext_wait_queue *wq_get_first_waiter(
  414. struct mqueue_inode_info *info, int sr)
  415. {
  416. struct list_head *ptr;
  417. ptr = info->e_wait_q[sr].list.prev;
  418. if (ptr == &info->e_wait_q[sr].list)
  419. return NULL;
  420. return list_entry(ptr, struct ext_wait_queue, list);
  421. }
  422. /* Auxiliary functions to manipulate messages' list */
  423. static void msg_insert(struct msg_msg *ptr, struct mqueue_inode_info *info)
  424. {
  425. int k;
  426. k = info->attr.mq_curmsgs - 1;
  427. while (k >= 0 && info->messages[k]->m_type >= ptr->m_type) {
  428. info->messages[k + 1] = info->messages[k];
  429. k--;
  430. }
  431. info->attr.mq_curmsgs++;
  432. info->qsize += ptr->m_ts;
  433. info->messages[k + 1] = ptr;
  434. }
  435. static inline struct msg_msg *msg_get(struct mqueue_inode_info *info)
  436. {
  437. info->qsize -= info->messages[--info->attr.mq_curmsgs]->m_ts;
  438. return info->messages[info->attr.mq_curmsgs];
  439. }
  440. static inline void set_cookie(struct sk_buff *skb, char code)
  441. {
  442. ((char*)skb->data)[NOTIFY_COOKIE_LEN-1] = code;
  443. }
  444. /*
  445. * The next function is only to split too long sys_mq_timedsend
  446. */
  447. static void __do_notify(struct mqueue_inode_info *info)
  448. {
  449. /* notification
  450. * invoked when there is registered process and there isn't process
  451. * waiting synchronously for message AND state of queue changed from
  452. * empty to not empty. Here we are sure that no one is waiting
  453. * synchronously. */
  454. if (info->notify_owner &&
  455. info->attr.mq_curmsgs == 1) {
  456. struct siginfo sig_i;
  457. switch (info->notify.sigev_notify) {
  458. case SIGEV_NONE:
  459. break;
  460. case SIGEV_SIGNAL:
  461. /* sends signal */
  462. sig_i.si_signo = info->notify.sigev_signo;
  463. sig_i.si_errno = 0;
  464. sig_i.si_code = SI_MESGQ;
  465. sig_i.si_value = info->notify.sigev_value;
  466. /* map current pid/uid into info->owner's namespaces */
  467. rcu_read_lock();
  468. sig_i.si_pid = task_tgid_nr_ns(current,
  469. ns_of_pid(info->notify_owner));
  470. sig_i.si_uid = user_ns_map_uid(info->user->user_ns,
  471. current_cred(), current_uid());
  472. rcu_read_unlock();
  473. kill_pid_info(info->notify.sigev_signo,
  474. &sig_i, info->notify_owner);
  475. break;
  476. case SIGEV_THREAD:
  477. set_cookie(info->notify_cookie, NOTIFY_WOKENUP);
  478. netlink_sendskb(info->notify_sock, info->notify_cookie);
  479. break;
  480. }
  481. /* after notification unregisters process */
  482. put_pid(info->notify_owner);
  483. info->notify_owner = NULL;
  484. }
  485. wake_up(&info->wait_q);
  486. }
  487. static int prepare_timeout(const struct timespec __user *u_abs_timeout,
  488. ktime_t *expires, struct timespec *ts)
  489. {
  490. if (copy_from_user(ts, u_abs_timeout, sizeof(struct timespec)))
  491. return -EFAULT;
  492. if (!timespec_valid(ts))
  493. return -EINVAL;
  494. *expires = timespec_to_ktime(*ts);
  495. return 0;
  496. }
  497. static void remove_notification(struct mqueue_inode_info *info)
  498. {
  499. if (info->notify_owner != NULL &&
  500. info->notify.sigev_notify == SIGEV_THREAD) {
  501. set_cookie(info->notify_cookie, NOTIFY_REMOVED);
  502. netlink_sendskb(info->notify_sock, info->notify_cookie);
  503. }
  504. put_pid(info->notify_owner);
  505. info->notify_owner = NULL;
  506. }
  507. static int mq_attr_ok(struct ipc_namespace *ipc_ns, struct mq_attr *attr)
  508. {
  509. if (attr->mq_maxmsg <= 0 || attr->mq_msgsize <= 0)
  510. return 0;
  511. if (capable(CAP_SYS_RESOURCE)) {
  512. if (attr->mq_maxmsg > HARD_MSGMAX)
  513. return 0;
  514. } else {
  515. if (attr->mq_maxmsg > ipc_ns->mq_msg_max ||
  516. attr->mq_msgsize > ipc_ns->mq_msgsize_max)
  517. return 0;
  518. }
  519. /* check for overflow */
  520. if (attr->mq_msgsize > ULONG_MAX/attr->mq_maxmsg)
  521. return 0;
  522. if ((unsigned long)(attr->mq_maxmsg * (attr->mq_msgsize
  523. + sizeof (struct msg_msg *))) <
  524. (unsigned long)(attr->mq_maxmsg * attr->mq_msgsize))
  525. return 0;
  526. return 1;
  527. }
  528. /*
  529. * Invoked when creating a new queue via sys_mq_open
  530. */
  531. static struct file *do_create(struct ipc_namespace *ipc_ns, struct dentry *dir,
  532. struct dentry *dentry, int oflag, umode_t mode,
  533. struct mq_attr *attr)
  534. {
  535. const struct cred *cred = current_cred();
  536. struct file *result;
  537. int ret;
  538. if (attr) {
  539. if (!mq_attr_ok(ipc_ns, attr)) {
  540. ret = -EINVAL;
  541. goto out;
  542. }
  543. /* store for use during create */
  544. dentry->d_fsdata = attr;
  545. }
  546. mode &= ~current_umask();
  547. ret = mnt_want_write(ipc_ns->mq_mnt);
  548. if (ret)
  549. goto out;
  550. ret = vfs_create(dir->d_inode, dentry, mode, NULL);
  551. dentry->d_fsdata = NULL;
  552. if (ret)
  553. goto out_drop_write;
  554. result = dentry_open(dentry, ipc_ns->mq_mnt, oflag, cred);
  555. /*
  556. * dentry_open() took a persistent mnt_want_write(),
  557. * so we can now drop this one.
  558. */
  559. mnt_drop_write(ipc_ns->mq_mnt);
  560. return result;
  561. out_drop_write:
  562. mnt_drop_write(ipc_ns->mq_mnt);
  563. out:
  564. dput(dentry);
  565. mntput(ipc_ns->mq_mnt);
  566. return ERR_PTR(ret);
  567. }
  568. /* Opens existing queue */
  569. static struct file *do_open(struct ipc_namespace *ipc_ns,
  570. struct dentry *dentry, int oflag)
  571. {
  572. int ret;
  573. const struct cred *cred = current_cred();
  574. static const int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE,
  575. MAY_READ | MAY_WRITE };
  576. if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY)) {
  577. ret = -EINVAL;
  578. goto err;
  579. }
  580. if (inode_permission(dentry->d_inode, oflag2acc[oflag & O_ACCMODE])) {
  581. ret = -EACCES;
  582. goto err;
  583. }
  584. return dentry_open(dentry, ipc_ns->mq_mnt, oflag, cred);
  585. err:
  586. dput(dentry);
  587. mntput(ipc_ns->mq_mnt);
  588. return ERR_PTR(ret);
  589. }
  590. SYSCALL_DEFINE4(mq_open, const char __user *, u_name, int, oflag, umode_t, mode,
  591. struct mq_attr __user *, u_attr)
  592. {
  593. struct dentry *dentry;
  594. struct file *filp;
  595. char *name;
  596. struct mq_attr attr;
  597. int fd, error;
  598. struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
  599. if (u_attr && copy_from_user(&attr, u_attr, sizeof(struct mq_attr)))
  600. return -EFAULT;
  601. audit_mq_open(oflag, mode, u_attr ? &attr : NULL);
  602. if (IS_ERR(name = getname(u_name)))
  603. return PTR_ERR(name);
  604. fd = get_unused_fd_flags(O_CLOEXEC);
  605. if (fd < 0)
  606. goto out_putname;
  607. mutex_lock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
  608. dentry = lookup_one_len(name, ipc_ns->mq_mnt->mnt_root, strlen(name));
  609. if (IS_ERR(dentry)) {
  610. error = PTR_ERR(dentry);
  611. goto out_putfd;
  612. }
  613. mntget(ipc_ns->mq_mnt);
  614. if (oflag & O_CREAT) {
  615. if (dentry->d_inode) { /* entry already exists */
  616. audit_inode(name, dentry);
  617. if (oflag & O_EXCL) {
  618. error = -EEXIST;
  619. goto out;
  620. }
  621. filp = do_open(ipc_ns, dentry, oflag);
  622. } else {
  623. filp = do_create(ipc_ns, ipc_ns->mq_mnt->mnt_root,
  624. dentry, oflag, mode,
  625. u_attr ? &attr : NULL);
  626. }
  627. } else {
  628. if (!dentry->d_inode) {
  629. error = -ENOENT;
  630. goto out;
  631. }
  632. audit_inode(name, dentry);
  633. filp = do_open(ipc_ns, dentry, oflag);
  634. }
  635. if (IS_ERR(filp)) {
  636. error = PTR_ERR(filp);
  637. goto out_putfd;
  638. }
  639. fd_install(fd, filp);
  640. goto out_upsem;
  641. out:
  642. dput(dentry);
  643. mntput(ipc_ns->mq_mnt);
  644. out_putfd:
  645. put_unused_fd(fd);
  646. fd = error;
  647. out_upsem:
  648. mutex_unlock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
  649. out_putname:
  650. putname(name);
  651. return fd;
  652. }
  653. SYSCALL_DEFINE1(mq_unlink, const char __user *, u_name)
  654. {
  655. int err;
  656. char *name;
  657. struct dentry *dentry;
  658. struct inode *inode = NULL;
  659. struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
  660. name = getname(u_name);
  661. if (IS_ERR(name))
  662. return PTR_ERR(name);
  663. mutex_lock_nested(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex,
  664. I_MUTEX_PARENT);
  665. dentry = lookup_one_len(name, ipc_ns->mq_mnt->mnt_root, strlen(name));
  666. if (IS_ERR(dentry)) {
  667. err = PTR_ERR(dentry);
  668. goto out_unlock;
  669. }
  670. if (!dentry->d_inode) {
  671. err = -ENOENT;
  672. goto out_err;
  673. }
  674. inode = dentry->d_inode;
  675. if (inode)
  676. ihold(inode);
  677. err = mnt_want_write(ipc_ns->mq_mnt);
  678. if (err)
  679. goto out_err;
  680. err = vfs_unlink(dentry->d_parent->d_inode, dentry);
  681. mnt_drop_write(ipc_ns->mq_mnt);
  682. out_err:
  683. dput(dentry);
  684. out_unlock:
  685. mutex_unlock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
  686. putname(name);
  687. if (inode)
  688. iput(inode);
  689. return err;
  690. }
  691. /* Pipelined send and receive functions.
  692. *
  693. * If a receiver finds no waiting message, then it registers itself in the
  694. * list of waiting receivers. A sender checks that list before adding the new
  695. * message into the message array. If there is a waiting receiver, then it
  696. * bypasses the message array and directly hands the message over to the
  697. * receiver.
  698. * The receiver accepts the message and returns without grabbing the queue
  699. * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers
  700. * are necessary. The same algorithm is used for sysv semaphores, see
  701. * ipc/sem.c for more details.
  702. *
  703. * The same algorithm is used for senders.
  704. */
  705. /* pipelined_send() - send a message directly to the task waiting in
  706. * sys_mq_timedreceive() (without inserting message into a queue).
  707. */
  708. static inline void pipelined_send(struct mqueue_inode_info *info,
  709. struct msg_msg *message,
  710. struct ext_wait_queue *receiver)
  711. {
  712. receiver->msg = message;
  713. list_del(&receiver->list);
  714. receiver->state = STATE_PENDING;
  715. wake_up_process(receiver->task);
  716. smp_wmb();
  717. receiver->state = STATE_READY;
  718. }
  719. /* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
  720. * gets its message and put to the queue (we have one free place for sure). */
  721. static inline void pipelined_receive(struct mqueue_inode_info *info)
  722. {
  723. struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
  724. if (!sender) {
  725. /* for poll */
  726. wake_up_interruptible(&info->wait_q);
  727. return;
  728. }
  729. msg_insert(sender->msg, info);
  730. list_del(&sender->list);
  731. sender->state = STATE_PENDING;
  732. wake_up_process(sender->task);
  733. smp_wmb();
  734. sender->state = STATE_READY;
  735. }
  736. SYSCALL_DEFINE5(mq_timedsend, mqd_t, mqdes, const char __user *, u_msg_ptr,
  737. size_t, msg_len, unsigned int, msg_prio,
  738. const struct timespec __user *, u_abs_timeout)
  739. {
  740. struct file *filp;
  741. struct inode *inode;
  742. struct ext_wait_queue wait;
  743. struct ext_wait_queue *receiver;
  744. struct msg_msg *msg_ptr;
  745. struct mqueue_inode_info *info;
  746. ktime_t expires, *timeout = NULL;
  747. struct timespec ts;
  748. int ret;
  749. if (u_abs_timeout) {
  750. int res = prepare_timeout(u_abs_timeout, &expires, &ts);
  751. if (res)
  752. return res;
  753. timeout = &expires;
  754. }
  755. if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX))
  756. return -EINVAL;
  757. audit_mq_sendrecv(mqdes, msg_len, msg_prio, timeout ? &ts : NULL);
  758. filp = fget(mqdes);
  759. if (unlikely(!filp)) {
  760. ret = -EBADF;
  761. goto out;
  762. }
  763. inode = filp->f_path.dentry->d_inode;
  764. if (unlikely(filp->f_op != &mqueue_file_operations)) {
  765. ret = -EBADF;
  766. goto out_fput;
  767. }
  768. info = MQUEUE_I(inode);
  769. audit_inode(NULL, filp->f_path.dentry);
  770. if (unlikely(!(filp->f_mode & FMODE_WRITE))) {
  771. ret = -EBADF;
  772. goto out_fput;
  773. }
  774. if (unlikely(msg_len > info->attr.mq_msgsize)) {
  775. ret = -EMSGSIZE;
  776. goto out_fput;
  777. }
  778. /* First try to allocate memory, before doing anything with
  779. * existing queues. */
  780. msg_ptr = load_msg(u_msg_ptr, msg_len);
  781. if (IS_ERR(msg_ptr)) {
  782. ret = PTR_ERR(msg_ptr);
  783. goto out_fput;
  784. }
  785. msg_ptr->m_ts = msg_len;
  786. msg_ptr->m_type = msg_prio;
  787. spin_lock(&info->lock);
  788. if (info->attr.mq_curmsgs == info->attr.mq_maxmsg) {
  789. if (filp->f_flags & O_NONBLOCK) {
  790. spin_unlock(&info->lock);
  791. ret = -EAGAIN;
  792. } else {
  793. wait.task = current;
  794. wait.msg = (void *) msg_ptr;
  795. wait.state = STATE_NONE;
  796. ret = wq_sleep(info, SEND, timeout, &wait);
  797. }
  798. if (ret < 0)
  799. free_msg(msg_ptr);
  800. } else {
  801. receiver = wq_get_first_waiter(info, RECV);
  802. if (receiver) {
  803. pipelined_send(info, msg_ptr, receiver);
  804. } else {
  805. /* adds message to the queue */
  806. msg_insert(msg_ptr, info);
  807. __do_notify(info);
  808. }
  809. inode->i_atime = inode->i_mtime = inode->i_ctime =
  810. CURRENT_TIME;
  811. spin_unlock(&info->lock);
  812. ret = 0;
  813. }
  814. out_fput:
  815. fput(filp);
  816. out:
  817. return ret;
  818. }
  819. SYSCALL_DEFINE5(mq_timedreceive, mqd_t, mqdes, char __user *, u_msg_ptr,
  820. size_t, msg_len, unsigned int __user *, u_msg_prio,
  821. const struct timespec __user *, u_abs_timeout)
  822. {
  823. ssize_t ret;
  824. struct msg_msg *msg_ptr;
  825. struct file *filp;
  826. struct inode *inode;
  827. struct mqueue_inode_info *info;
  828. struct ext_wait_queue wait;
  829. ktime_t expires, *timeout = NULL;
  830. struct timespec ts;
  831. if (u_abs_timeout) {
  832. int res = prepare_timeout(u_abs_timeout, &expires, &ts);
  833. if (res)
  834. return res;
  835. timeout = &expires;
  836. }
  837. audit_mq_sendrecv(mqdes, msg_len, 0, timeout ? &ts : NULL);
  838. filp = fget(mqdes);
  839. if (unlikely(!filp)) {
  840. ret = -EBADF;
  841. goto out;
  842. }
  843. inode = filp->f_path.dentry->d_inode;
  844. if (unlikely(filp->f_op != &mqueue_file_operations)) {
  845. ret = -EBADF;
  846. goto out_fput;
  847. }
  848. info = MQUEUE_I(inode);
  849. audit_inode(NULL, filp->f_path.dentry);
  850. if (unlikely(!(filp->f_mode & FMODE_READ))) {
  851. ret = -EBADF;
  852. goto out_fput;
  853. }
  854. /* checks if buffer is big enough */
  855. if (unlikely(msg_len < info->attr.mq_msgsize)) {
  856. ret = -EMSGSIZE;
  857. goto out_fput;
  858. }
  859. spin_lock(&info->lock);
  860. if (info->attr.mq_curmsgs == 0) {
  861. if (filp->f_flags & O_NONBLOCK) {
  862. spin_unlock(&info->lock);
  863. ret = -EAGAIN;
  864. } else {
  865. wait.task = current;
  866. wait.state = STATE_NONE;
  867. ret = wq_sleep(info, RECV, timeout, &wait);
  868. msg_ptr = wait.msg;
  869. }
  870. } else {
  871. msg_ptr = msg_get(info);
  872. inode->i_atime = inode->i_mtime = inode->i_ctime =
  873. CURRENT_TIME;
  874. /* There is now free space in queue. */
  875. pipelined_receive(info);
  876. spin_unlock(&info->lock);
  877. ret = 0;
  878. }
  879. if (ret == 0) {
  880. ret = msg_ptr->m_ts;
  881. if ((u_msg_prio && put_user(msg_ptr->m_type, u_msg_prio)) ||
  882. store_msg(u_msg_ptr, msg_ptr, msg_ptr->m_ts)) {
  883. ret = -EFAULT;
  884. }
  885. free_msg(msg_ptr);
  886. }
  887. out_fput:
  888. fput(filp);
  889. out:
  890. return ret;
  891. }
  892. /*
  893. * Notes: the case when user wants us to deregister (with NULL as pointer)
  894. * and he isn't currently owner of notification, will be silently discarded.
  895. * It isn't explicitly defined in the POSIX.
  896. */
  897. SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
  898. const struct sigevent __user *, u_notification)
  899. {
  900. int ret;
  901. struct file *filp;
  902. struct sock *sock;
  903. struct inode *inode;
  904. struct sigevent notification;
  905. struct mqueue_inode_info *info;
  906. struct sk_buff *nc;
  907. if (u_notification) {
  908. if (copy_from_user(&notification, u_notification,
  909. sizeof(struct sigevent)))
  910. return -EFAULT;
  911. }
  912. audit_mq_notify(mqdes, u_notification ? &notification : NULL);
  913. nc = NULL;
  914. sock = NULL;
  915. if (u_notification != NULL) {
  916. if (unlikely(notification.sigev_notify != SIGEV_NONE &&
  917. notification.sigev_notify != SIGEV_SIGNAL &&
  918. notification.sigev_notify != SIGEV_THREAD))
  919. return -EINVAL;
  920. if (notification.sigev_notify == SIGEV_SIGNAL &&
  921. !valid_signal(notification.sigev_signo)) {
  922. return -EINVAL;
  923. }
  924. if (notification.sigev_notify == SIGEV_THREAD) {
  925. long timeo;
  926. /* create the notify skb */
  927. nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL);
  928. if (!nc) {
  929. ret = -ENOMEM;
  930. goto out;
  931. }
  932. if (copy_from_user(nc->data,
  933. notification.sigev_value.sival_ptr,
  934. NOTIFY_COOKIE_LEN)) {
  935. ret = -EFAULT;
  936. goto out;
  937. }
  938. /* TODO: add a header? */
  939. skb_put(nc, NOTIFY_COOKIE_LEN);
  940. /* and attach it to the socket */
  941. retry:
  942. filp = fget(notification.sigev_signo);
  943. if (!filp) {
  944. ret = -EBADF;
  945. goto out;
  946. }
  947. sock = netlink_getsockbyfilp(filp);
  948. fput(filp);
  949. if (IS_ERR(sock)) {
  950. ret = PTR_ERR(sock);
  951. sock = NULL;
  952. goto out;
  953. }
  954. timeo = MAX_SCHEDULE_TIMEOUT;
  955. ret = netlink_attachskb(sock, nc, &timeo, NULL);
  956. if (ret == 1)
  957. goto retry;
  958. if (ret) {
  959. sock = NULL;
  960. nc = NULL;
  961. goto out;
  962. }
  963. }
  964. }
  965. filp = fget(mqdes);
  966. if (!filp) {
  967. ret = -EBADF;
  968. goto out;
  969. }
  970. inode = filp->f_path.dentry->d_inode;
  971. if (unlikely(filp->f_op != &mqueue_file_operations)) {
  972. ret = -EBADF;
  973. goto out_fput;
  974. }
  975. info = MQUEUE_I(inode);
  976. ret = 0;
  977. spin_lock(&info->lock);
  978. if (u_notification == NULL) {
  979. if (info->notify_owner == task_tgid(current)) {
  980. remove_notification(info);
  981. inode->i_atime = inode->i_ctime = CURRENT_TIME;
  982. }
  983. } else if (info->notify_owner != NULL) {
  984. ret = -EBUSY;
  985. } else {
  986. switch (notification.sigev_notify) {
  987. case SIGEV_NONE:
  988. info->notify.sigev_notify = SIGEV_NONE;
  989. break;
  990. case SIGEV_THREAD:
  991. info->notify_sock = sock;
  992. info->notify_cookie = nc;
  993. sock = NULL;
  994. nc = NULL;
  995. info->notify.sigev_notify = SIGEV_THREAD;
  996. break;
  997. case SIGEV_SIGNAL:
  998. info->notify.sigev_signo = notification.sigev_signo;
  999. info->notify.sigev_value = notification.sigev_value;
  1000. info->notify.sigev_notify = SIGEV_SIGNAL;
  1001. break;
  1002. }
  1003. info->notify_owner = get_pid(task_tgid(current));
  1004. inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1005. }
  1006. spin_unlock(&info->lock);
  1007. out_fput:
  1008. fput(filp);
  1009. out:
  1010. if (sock) {
  1011. netlink_detachskb(sock, nc);
  1012. } else if (nc) {
  1013. dev_kfree_skb(nc);
  1014. }
  1015. return ret;
  1016. }
  1017. SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
  1018. const struct mq_attr __user *, u_mqstat,
  1019. struct mq_attr __user *, u_omqstat)
  1020. {
  1021. int ret;
  1022. struct mq_attr mqstat, omqstat;
  1023. struct file *filp;
  1024. struct inode *inode;
  1025. struct mqueue_inode_info *info;
  1026. if (u_mqstat != NULL) {
  1027. if (copy_from_user(&mqstat, u_mqstat, sizeof(struct mq_attr)))
  1028. return -EFAULT;
  1029. if (mqstat.mq_flags & (~O_NONBLOCK))
  1030. return -EINVAL;
  1031. }
  1032. filp = fget(mqdes);
  1033. if (!filp) {
  1034. ret = -EBADF;
  1035. goto out;
  1036. }
  1037. inode = filp->f_path.dentry->d_inode;
  1038. if (unlikely(filp->f_op != &mqueue_file_operations)) {
  1039. ret = -EBADF;
  1040. goto out_fput;
  1041. }
  1042. info = MQUEUE_I(inode);
  1043. spin_lock(&info->lock);
  1044. omqstat = info->attr;
  1045. omqstat.mq_flags = filp->f_flags & O_NONBLOCK;
  1046. if (u_mqstat) {
  1047. audit_mq_getsetattr(mqdes, &mqstat);
  1048. spin_lock(&filp->f_lock);
  1049. if (mqstat.mq_flags & O_NONBLOCK)
  1050. filp->f_flags |= O_NONBLOCK;
  1051. else
  1052. filp->f_flags &= ~O_NONBLOCK;
  1053. spin_unlock(&filp->f_lock);
  1054. inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1055. }
  1056. spin_unlock(&info->lock);
  1057. ret = 0;
  1058. if (u_omqstat != NULL && copy_to_user(u_omqstat, &omqstat,
  1059. sizeof(struct mq_attr)))
  1060. ret = -EFAULT;
  1061. out_fput:
  1062. fput(filp);
  1063. out:
  1064. return ret;
  1065. }
  1066. static const struct inode_operations mqueue_dir_inode_operations = {
  1067. .lookup = simple_lookup,
  1068. .create = mqueue_create,
  1069. .unlink = mqueue_unlink,
  1070. };
  1071. static const struct file_operations mqueue_file_operations = {
  1072. .flush = mqueue_flush_file,
  1073. .poll = mqueue_poll_file,
  1074. .read = mqueue_read_file,
  1075. .llseek = default_llseek,
  1076. };
  1077. static const struct super_operations mqueue_super_ops = {
  1078. .alloc_inode = mqueue_alloc_inode,
  1079. .destroy_inode = mqueue_destroy_inode,
  1080. .evict_inode = mqueue_evict_inode,
  1081. .statfs = simple_statfs,
  1082. };
  1083. static struct file_system_type mqueue_fs_type = {
  1084. .name = "mqueue",
  1085. .mount = mqueue_mount,
  1086. .kill_sb = kill_litter_super,
  1087. };
  1088. int mq_init_ns(struct ipc_namespace *ns)
  1089. {
  1090. ns->mq_queues_count = 0;
  1091. ns->mq_queues_max = DFLT_QUEUESMAX;
  1092. ns->mq_msg_max = DFLT_MSGMAX;
  1093. ns->mq_msgsize_max = DFLT_MSGSIZEMAX;
  1094. ns->mq_mnt = kern_mount_data(&mqueue_fs_type, ns);
  1095. if (IS_ERR(ns->mq_mnt)) {
  1096. int err = PTR_ERR(ns->mq_mnt);
  1097. ns->mq_mnt = NULL;
  1098. return err;
  1099. }
  1100. return 0;
  1101. }
  1102. void mq_clear_sbinfo(struct ipc_namespace *ns)
  1103. {
  1104. ns->mq_mnt->mnt_sb->s_fs_info = NULL;
  1105. }
  1106. void mq_put_mnt(struct ipc_namespace *ns)
  1107. {
  1108. kern_unmount(ns->mq_mnt);
  1109. }
  1110. static int __init init_mqueue_fs(void)
  1111. {
  1112. int error;
  1113. mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache",
  1114. sizeof(struct mqueue_inode_info), 0,
  1115. SLAB_HWCACHE_ALIGN, init_once);
  1116. if (mqueue_inode_cachep == NULL)
  1117. return -ENOMEM;
  1118. /* ignore failures - they are not fatal */
  1119. mq_sysctl_table = mq_register_sysctl_table();
  1120. error = register_filesystem(&mqueue_fs_type);
  1121. if (error)
  1122. goto out_sysctl;
  1123. spin_lock_init(&mq_lock);
  1124. error = mq_init_ns(&init_ipc_ns);
  1125. if (error)
  1126. goto out_filesystem;
  1127. return 0;
  1128. out_filesystem:
  1129. unregister_filesystem(&mqueue_fs_type);
  1130. out_sysctl:
  1131. if (mq_sysctl_table)
  1132. unregister_sysctl_table(mq_sysctl_table);
  1133. kmem_cache_destroy(mqueue_inode_cachep);
  1134. return error;
  1135. }
  1136. __initcall(init_mqueue_fs);