mqueue.c 29 KB

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