mqueue.c 31 KB

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