mqueue.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  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 mq_attr attr;
  516. int ret;
  517. if (u_attr) {
  518. ret = -EFAULT;
  519. if (copy_from_user(&attr, u_attr, sizeof(attr)))
  520. goto out;
  521. ret = -EINVAL;
  522. if (!mq_attr_ok(&attr))
  523. goto out;
  524. /* store for use during create */
  525. dentry->d_fsdata = &attr;
  526. }
  527. mode &= ~current->fs->umask;
  528. ret = vfs_create(dir->d_inode, dentry, mode, NULL);
  529. dentry->d_fsdata = NULL;
  530. if (ret)
  531. goto out;
  532. return dentry_open(dentry, mqueue_mnt, oflag);
  533. out:
  534. dput(dentry);
  535. mntput(mqueue_mnt);
  536. return ERR_PTR(ret);
  537. }
  538. /* Opens existing queue */
  539. static struct file *do_open(struct dentry *dentry, int oflag)
  540. {
  541. static int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE,
  542. MAY_READ | MAY_WRITE };
  543. if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY)) {
  544. dput(dentry);
  545. mntput(mqueue_mnt);
  546. return ERR_PTR(-EINVAL);
  547. }
  548. if (permission(dentry->d_inode, oflag2acc[oflag & O_ACCMODE], NULL)) {
  549. dput(dentry);
  550. mntput(mqueue_mnt);
  551. return ERR_PTR(-EACCES);
  552. }
  553. return dentry_open(dentry, mqueue_mnt, oflag);
  554. }
  555. asmlinkage long sys_mq_open(const char __user *u_name, int oflag, mode_t mode,
  556. struct mq_attr __user *u_attr)
  557. {
  558. struct dentry *dentry;
  559. struct file *filp;
  560. char *name;
  561. int fd, error;
  562. if (IS_ERR(name = getname(u_name)))
  563. return PTR_ERR(name);
  564. fd = get_unused_fd();
  565. if (fd < 0)
  566. goto out_putname;
  567. mutex_lock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
  568. dentry = lookup_one_len(name, mqueue_mnt->mnt_root, strlen(name));
  569. if (IS_ERR(dentry)) {
  570. error = PTR_ERR(dentry);
  571. goto out_err;
  572. }
  573. mntget(mqueue_mnt);
  574. if (oflag & O_CREAT) {
  575. if (dentry->d_inode) { /* entry already exists */
  576. error = -EEXIST;
  577. if (oflag & O_EXCL)
  578. goto out;
  579. filp = do_open(dentry, oflag);
  580. } else {
  581. filp = do_create(mqueue_mnt->mnt_root, dentry,
  582. oflag, mode, u_attr);
  583. }
  584. } else {
  585. error = -ENOENT;
  586. if (!dentry->d_inode)
  587. goto out;
  588. filp = do_open(dentry, oflag);
  589. }
  590. if (IS_ERR(filp)) {
  591. error = PTR_ERR(filp);
  592. goto out_putfd;
  593. }
  594. set_close_on_exec(fd, 1);
  595. fd_install(fd, filp);
  596. goto out_upsem;
  597. out:
  598. dput(dentry);
  599. mntput(mqueue_mnt);
  600. out_putfd:
  601. put_unused_fd(fd);
  602. out_err:
  603. fd = error;
  604. out_upsem:
  605. mutex_unlock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
  606. out_putname:
  607. putname(name);
  608. return fd;
  609. }
  610. asmlinkage long sys_mq_unlink(const char __user *u_name)
  611. {
  612. int err;
  613. char *name;
  614. struct dentry *dentry;
  615. struct inode *inode = NULL;
  616. name = getname(u_name);
  617. if (IS_ERR(name))
  618. return PTR_ERR(name);
  619. mutex_lock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
  620. dentry = lookup_one_len(name, mqueue_mnt->mnt_root, strlen(name));
  621. if (IS_ERR(dentry)) {
  622. err = PTR_ERR(dentry);
  623. goto out_unlock;
  624. }
  625. if (!dentry->d_inode) {
  626. err = -ENOENT;
  627. goto out_err;
  628. }
  629. inode = dentry->d_inode;
  630. if (inode)
  631. atomic_inc(&inode->i_count);
  632. err = vfs_unlink(dentry->d_parent->d_inode, dentry);
  633. out_err:
  634. dput(dentry);
  635. out_unlock:
  636. mutex_unlock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
  637. putname(name);
  638. if (inode)
  639. iput(inode);
  640. return err;
  641. }
  642. /* Pipelined send and receive functions.
  643. *
  644. * If a receiver finds no waiting message, then it registers itself in the
  645. * list of waiting receivers. A sender checks that list before adding the new
  646. * message into the message array. If there is a waiting receiver, then it
  647. * bypasses the message array and directly hands the message over to the
  648. * receiver.
  649. * The receiver accepts the message and returns without grabbing the queue
  650. * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers
  651. * are necessary. The same algorithm is used for sysv semaphores, see
  652. * ipc/sem.c fore more details.
  653. *
  654. * The same algorithm is used for senders.
  655. */
  656. /* pipelined_send() - send a message directly to the task waiting in
  657. * sys_mq_timedreceive() (without inserting message into a queue).
  658. */
  659. static inline void pipelined_send(struct mqueue_inode_info *info,
  660. struct msg_msg *message,
  661. struct ext_wait_queue *receiver)
  662. {
  663. receiver->msg = message;
  664. list_del(&receiver->list);
  665. receiver->state = STATE_PENDING;
  666. wake_up_process(receiver->task);
  667. smp_wmb();
  668. receiver->state = STATE_READY;
  669. }
  670. /* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
  671. * gets its message and put to the queue (we have one free place for sure). */
  672. static inline void pipelined_receive(struct mqueue_inode_info *info)
  673. {
  674. struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
  675. if (!sender) {
  676. /* for poll */
  677. wake_up_interruptible(&info->wait_q);
  678. return;
  679. }
  680. msg_insert(sender->msg, info);
  681. list_del(&sender->list);
  682. sender->state = STATE_PENDING;
  683. wake_up_process(sender->task);
  684. smp_wmb();
  685. sender->state = STATE_READY;
  686. }
  687. asmlinkage long sys_mq_timedsend(mqd_t mqdes, const char __user *u_msg_ptr,
  688. size_t msg_len, unsigned int msg_prio,
  689. const struct timespec __user *u_abs_timeout)
  690. {
  691. struct file *filp;
  692. struct inode *inode;
  693. struct ext_wait_queue wait;
  694. struct ext_wait_queue *receiver;
  695. struct msg_msg *msg_ptr;
  696. struct mqueue_inode_info *info;
  697. long timeout;
  698. int ret;
  699. if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX))
  700. return -EINVAL;
  701. timeout = prepare_timeout(u_abs_timeout);
  702. ret = -EBADF;
  703. filp = fget(mqdes);
  704. if (unlikely(!filp))
  705. goto out;
  706. inode = filp->f_dentry->d_inode;
  707. if (unlikely(filp->f_op != &mqueue_file_operations))
  708. goto out_fput;
  709. info = MQUEUE_I(inode);
  710. if (unlikely(!(filp->f_mode & FMODE_WRITE)))
  711. goto out_fput;
  712. if (unlikely(msg_len > info->attr.mq_msgsize)) {
  713. ret = -EMSGSIZE;
  714. goto out_fput;
  715. }
  716. /* First try to allocate memory, before doing anything with
  717. * existing queues. */
  718. msg_ptr = load_msg(u_msg_ptr, msg_len);
  719. if (IS_ERR(msg_ptr)) {
  720. ret = PTR_ERR(msg_ptr);
  721. goto out_fput;
  722. }
  723. msg_ptr->m_ts = msg_len;
  724. msg_ptr->m_type = msg_prio;
  725. spin_lock(&info->lock);
  726. if (info->attr.mq_curmsgs == info->attr.mq_maxmsg) {
  727. if (filp->f_flags & O_NONBLOCK) {
  728. spin_unlock(&info->lock);
  729. ret = -EAGAIN;
  730. } else if (unlikely(timeout < 0)) {
  731. spin_unlock(&info->lock);
  732. ret = timeout;
  733. } else {
  734. wait.task = current;
  735. wait.msg = (void *) msg_ptr;
  736. wait.state = STATE_NONE;
  737. ret = wq_sleep(info, SEND, timeout, &wait);
  738. }
  739. if (ret < 0)
  740. free_msg(msg_ptr);
  741. } else {
  742. receiver = wq_get_first_waiter(info, RECV);
  743. if (receiver) {
  744. pipelined_send(info, msg_ptr, receiver);
  745. } else {
  746. /* adds message to the queue */
  747. msg_insert(msg_ptr, info);
  748. __do_notify(info);
  749. }
  750. inode->i_atime = inode->i_mtime = inode->i_ctime =
  751. CURRENT_TIME;
  752. spin_unlock(&info->lock);
  753. ret = 0;
  754. }
  755. out_fput:
  756. fput(filp);
  757. out:
  758. return ret;
  759. }
  760. asmlinkage ssize_t sys_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr,
  761. size_t msg_len, unsigned int __user *u_msg_prio,
  762. const struct timespec __user *u_abs_timeout)
  763. {
  764. long timeout;
  765. ssize_t ret;
  766. struct msg_msg *msg_ptr;
  767. struct file *filp;
  768. struct inode *inode;
  769. struct mqueue_inode_info *info;
  770. struct ext_wait_queue wait;
  771. timeout = prepare_timeout(u_abs_timeout);
  772. ret = -EBADF;
  773. filp = fget(mqdes);
  774. if (unlikely(!filp))
  775. goto out;
  776. inode = filp->f_dentry->d_inode;
  777. if (unlikely(filp->f_op != &mqueue_file_operations))
  778. goto out_fput;
  779. info = MQUEUE_I(inode);
  780. if (unlikely(!(filp->f_mode & FMODE_READ)))
  781. goto out_fput;
  782. /* checks if buffer is big enough */
  783. if (unlikely(msg_len < info->attr.mq_msgsize)) {
  784. ret = -EMSGSIZE;
  785. goto out_fput;
  786. }
  787. spin_lock(&info->lock);
  788. if (info->attr.mq_curmsgs == 0) {
  789. if (filp->f_flags & O_NONBLOCK) {
  790. spin_unlock(&info->lock);
  791. ret = -EAGAIN;
  792. msg_ptr = NULL;
  793. } else if (unlikely(timeout < 0)) {
  794. spin_unlock(&info->lock);
  795. ret = timeout;
  796. msg_ptr = NULL;
  797. } else {
  798. wait.task = current;
  799. wait.state = STATE_NONE;
  800. ret = wq_sleep(info, RECV, timeout, &wait);
  801. msg_ptr = wait.msg;
  802. }
  803. } else {
  804. msg_ptr = msg_get(info);
  805. inode->i_atime = inode->i_mtime = inode->i_ctime =
  806. CURRENT_TIME;
  807. /* There is now free space in queue. */
  808. pipelined_receive(info);
  809. spin_unlock(&info->lock);
  810. ret = 0;
  811. }
  812. if (ret == 0) {
  813. ret = msg_ptr->m_ts;
  814. if ((u_msg_prio && put_user(msg_ptr->m_type, u_msg_prio)) ||
  815. store_msg(u_msg_ptr, msg_ptr, msg_ptr->m_ts)) {
  816. ret = -EFAULT;
  817. }
  818. free_msg(msg_ptr);
  819. }
  820. out_fput:
  821. fput(filp);
  822. out:
  823. return ret;
  824. }
  825. /*
  826. * Notes: the case when user wants us to deregister (with NULL as pointer)
  827. * and he isn't currently owner of notification, will be silently discarded.
  828. * It isn't explicitly defined in the POSIX.
  829. */
  830. asmlinkage long sys_mq_notify(mqd_t mqdes,
  831. const struct sigevent __user *u_notification)
  832. {
  833. int ret;
  834. struct file *filp;
  835. struct sock *sock;
  836. struct inode *inode;
  837. struct sigevent notification;
  838. struct mqueue_inode_info *info;
  839. struct sk_buff *nc;
  840. nc = NULL;
  841. sock = NULL;
  842. if (u_notification != NULL) {
  843. if (copy_from_user(&notification, u_notification,
  844. sizeof(struct sigevent)))
  845. return -EFAULT;
  846. if (unlikely(notification.sigev_notify != SIGEV_NONE &&
  847. notification.sigev_notify != SIGEV_SIGNAL &&
  848. notification.sigev_notify != SIGEV_THREAD))
  849. return -EINVAL;
  850. if (notification.sigev_notify == SIGEV_SIGNAL &&
  851. !valid_signal(notification.sigev_signo)) {
  852. return -EINVAL;
  853. }
  854. if (notification.sigev_notify == SIGEV_THREAD) {
  855. /* create the notify skb */
  856. nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL);
  857. ret = -ENOMEM;
  858. if (!nc)
  859. goto out;
  860. ret = -EFAULT;
  861. if (copy_from_user(nc->data,
  862. notification.sigev_value.sival_ptr,
  863. NOTIFY_COOKIE_LEN)) {
  864. goto out;
  865. }
  866. /* TODO: add a header? */
  867. skb_put(nc, NOTIFY_COOKIE_LEN);
  868. /* and attach it to the socket */
  869. retry:
  870. filp = fget(notification.sigev_signo);
  871. ret = -EBADF;
  872. if (!filp)
  873. goto out;
  874. sock = netlink_getsockbyfilp(filp);
  875. fput(filp);
  876. if (IS_ERR(sock)) {
  877. ret = PTR_ERR(sock);
  878. sock = NULL;
  879. goto out;
  880. }
  881. ret = netlink_attachskb(sock, nc, 0, MAX_SCHEDULE_TIMEOUT);
  882. if (ret == 1)
  883. goto retry;
  884. if (ret) {
  885. sock = NULL;
  886. nc = NULL;
  887. goto out;
  888. }
  889. }
  890. }
  891. ret = -EBADF;
  892. filp = fget(mqdes);
  893. if (!filp)
  894. goto out;
  895. inode = filp->f_dentry->d_inode;
  896. if (unlikely(filp->f_op != &mqueue_file_operations))
  897. goto out_fput;
  898. info = MQUEUE_I(inode);
  899. ret = 0;
  900. spin_lock(&info->lock);
  901. if (u_notification == NULL) {
  902. if (info->notify_owner == current->tgid) {
  903. remove_notification(info);
  904. inode->i_atime = inode->i_ctime = CURRENT_TIME;
  905. }
  906. } else if (info->notify_owner != 0) {
  907. ret = -EBUSY;
  908. } else {
  909. switch (notification.sigev_notify) {
  910. case SIGEV_NONE:
  911. info->notify.sigev_notify = SIGEV_NONE;
  912. break;
  913. case SIGEV_THREAD:
  914. info->notify_sock = sock;
  915. info->notify_cookie = nc;
  916. sock = NULL;
  917. nc = NULL;
  918. info->notify.sigev_notify = SIGEV_THREAD;
  919. break;
  920. case SIGEV_SIGNAL:
  921. info->notify.sigev_signo = notification.sigev_signo;
  922. info->notify.sigev_value = notification.sigev_value;
  923. info->notify.sigev_notify = SIGEV_SIGNAL;
  924. break;
  925. }
  926. info->notify_owner = current->tgid;
  927. inode->i_atime = inode->i_ctime = CURRENT_TIME;
  928. }
  929. spin_unlock(&info->lock);
  930. out_fput:
  931. fput(filp);
  932. out:
  933. if (sock) {
  934. netlink_detachskb(sock, nc);
  935. } else if (nc) {
  936. dev_kfree_skb(nc);
  937. }
  938. return ret;
  939. }
  940. asmlinkage long sys_mq_getsetattr(mqd_t mqdes,
  941. const struct mq_attr __user *u_mqstat,
  942. struct mq_attr __user *u_omqstat)
  943. {
  944. int ret;
  945. struct mq_attr mqstat, omqstat;
  946. struct file *filp;
  947. struct inode *inode;
  948. struct mqueue_inode_info *info;
  949. if (u_mqstat != NULL) {
  950. if (copy_from_user(&mqstat, u_mqstat, sizeof(struct mq_attr)))
  951. return -EFAULT;
  952. if (mqstat.mq_flags & (~O_NONBLOCK))
  953. return -EINVAL;
  954. }
  955. ret = -EBADF;
  956. filp = fget(mqdes);
  957. if (!filp)
  958. goto out;
  959. inode = filp->f_dentry->d_inode;
  960. if (unlikely(filp->f_op != &mqueue_file_operations))
  961. goto out_fput;
  962. info = MQUEUE_I(inode);
  963. spin_lock(&info->lock);
  964. omqstat = info->attr;
  965. omqstat.mq_flags = filp->f_flags & O_NONBLOCK;
  966. if (u_mqstat) {
  967. if (mqstat.mq_flags & O_NONBLOCK)
  968. filp->f_flags |= O_NONBLOCK;
  969. else
  970. filp->f_flags &= ~O_NONBLOCK;
  971. inode->i_atime = inode->i_ctime = CURRENT_TIME;
  972. }
  973. spin_unlock(&info->lock);
  974. ret = 0;
  975. if (u_omqstat != NULL && copy_to_user(u_omqstat, &omqstat,
  976. sizeof(struct mq_attr)))
  977. ret = -EFAULT;
  978. out_fput:
  979. fput(filp);
  980. out:
  981. return ret;
  982. }
  983. static struct inode_operations mqueue_dir_inode_operations = {
  984. .lookup = simple_lookup,
  985. .create = mqueue_create,
  986. .unlink = mqueue_unlink,
  987. };
  988. static struct file_operations mqueue_file_operations = {
  989. .flush = mqueue_flush_file,
  990. .poll = mqueue_poll_file,
  991. .read = mqueue_read_file,
  992. };
  993. static struct super_operations mqueue_super_ops = {
  994. .alloc_inode = mqueue_alloc_inode,
  995. .destroy_inode = mqueue_destroy_inode,
  996. .statfs = simple_statfs,
  997. .delete_inode = mqueue_delete_inode,
  998. .drop_inode = generic_delete_inode,
  999. };
  1000. static struct file_system_type mqueue_fs_type = {
  1001. .name = "mqueue",
  1002. .get_sb = mqueue_get_sb,
  1003. .kill_sb = kill_litter_super,
  1004. };
  1005. static int msg_max_limit_min = DFLT_MSGMAX;
  1006. static int msg_max_limit_max = HARD_MSGMAX;
  1007. static int msg_maxsize_limit_min = DFLT_MSGSIZEMAX;
  1008. static int msg_maxsize_limit_max = INT_MAX;
  1009. static ctl_table mq_sysctls[] = {
  1010. {
  1011. .ctl_name = CTL_QUEUESMAX,
  1012. .procname = "queues_max",
  1013. .data = &queues_max,
  1014. .maxlen = sizeof(int),
  1015. .mode = 0644,
  1016. .proc_handler = &proc_dointvec,
  1017. },
  1018. {
  1019. .ctl_name = CTL_MSGMAX,
  1020. .procname = "msg_max",
  1021. .data = &msg_max,
  1022. .maxlen = sizeof(int),
  1023. .mode = 0644,
  1024. .proc_handler = &proc_dointvec_minmax,
  1025. .extra1 = &msg_max_limit_min,
  1026. .extra2 = &msg_max_limit_max,
  1027. },
  1028. {
  1029. .ctl_name = CTL_MSGSIZEMAX,
  1030. .procname = "msgsize_max",
  1031. .data = &msgsize_max,
  1032. .maxlen = sizeof(int),
  1033. .mode = 0644,
  1034. .proc_handler = &proc_dointvec_minmax,
  1035. .extra1 = &msg_maxsize_limit_min,
  1036. .extra2 = &msg_maxsize_limit_max,
  1037. },
  1038. { .ctl_name = 0 }
  1039. };
  1040. static ctl_table mq_sysctl_dir[] = {
  1041. {
  1042. .ctl_name = FS_MQUEUE,
  1043. .procname = "mqueue",
  1044. .mode = 0555,
  1045. .child = mq_sysctls,
  1046. },
  1047. { .ctl_name = 0 }
  1048. };
  1049. static ctl_table mq_sysctl_root[] = {
  1050. {
  1051. .ctl_name = CTL_FS,
  1052. .procname = "fs",
  1053. .mode = 0555,
  1054. .child = mq_sysctl_dir,
  1055. },
  1056. { .ctl_name = 0 }
  1057. };
  1058. static int __init init_mqueue_fs(void)
  1059. {
  1060. int error;
  1061. mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache",
  1062. sizeof(struct mqueue_inode_info), 0,
  1063. SLAB_HWCACHE_ALIGN, init_once, NULL);
  1064. if (mqueue_inode_cachep == NULL)
  1065. return -ENOMEM;
  1066. /* ignore failues - they are not fatal */
  1067. mq_sysctl_table = register_sysctl_table(mq_sysctl_root, 0);
  1068. error = register_filesystem(&mqueue_fs_type);
  1069. if (error)
  1070. goto out_sysctl;
  1071. if (IS_ERR(mqueue_mnt = kern_mount(&mqueue_fs_type))) {
  1072. error = PTR_ERR(mqueue_mnt);
  1073. goto out_filesystem;
  1074. }
  1075. /* internal initialization - not common for vfs */
  1076. queues_count = 0;
  1077. spin_lock_init(&mq_lock);
  1078. return 0;
  1079. out_filesystem:
  1080. unregister_filesystem(&mqueue_fs_type);
  1081. out_sysctl:
  1082. if (mq_sysctl_table)
  1083. unregister_sysctl_table(mq_sysctl_table);
  1084. if (kmem_cache_destroy(mqueue_inode_cachep)) {
  1085. printk(KERN_INFO
  1086. "mqueue_inode_cache: not all structures were freed\n");
  1087. }
  1088. return error;
  1089. }
  1090. __initcall(init_mqueue_fs);