mqueue.c 29 KB

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