mqueue.c 31 KB

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