mqueue.c 31 KB

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