mqueue.c 31 KB

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