mqueue.c 29 KB

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