mqueue.c 31 KB

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